2012-07-25 54 views
1

我一直在試圖讓我的BatteryPost類可以由alarmmanager在我的onCreate方法叫:BroadcastReceiever沒有得到調用

BatteryPost.java:

public class BatteryPost extends BroadcastReceiver{ 

@Override 
public void onReceive(Context context, Intent intent) { 
    HttpClient httpclient = new DefaultHttpClient(); 
    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 
    String batteryText = prefs.getString("battery", null); 
    String id = prefs.getString("id", null); 
    String URL = "http://mysite.com/checkin/index.php?device_uid="+id+"&bat="+batteryText; 
    { 
    try{ 
    String uri = URL.replace(" ", "%20"); //replace spaces with %20 
    Log.d("Checkin", uri); 
    HttpPost httppost = new HttpPost(uri); //post object 
    @SuppressWarnings("unused") 
    HttpResponse response = httpclient.execute(httppost); //execution 
    } catch (ClientProtocolException e) { 
     Log.d("Checkin", "ClientProtocolException"); 
    } catch (IOException i) { 
     Log.d("Checkin", "IOException"); 
    } 
    } 

} 
} 

應由AlarmManager被擊中在我的onCreate():

public void onCreate(Bundle savedInstanceState) { 
     /* 
     * Our onCreate block... 
     */ 
     super.onCreate(savedInstanceState); 
     Context context = getApplicationContext(); 
     setContentView(R.layout.activity_main); 
     Button submit = (Button)findViewById(R.id.submitButton); 
     submit.setOnClickListener(submitListener); //Initialization of the view and objects 
     this.registerReceiver(this.mBatInfoReceiver, 
     new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); 
     Calendar cal = Calendar.getInstance(); 
     cal.add(Calendar.MINUTE, 1); 
     Intent intent = new Intent(context, BatteryPost.class); 
     PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
     AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); 
     am.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 600000, sender); 

} 

當我打我註冊手動更新電池電量,以它的工作原理服務器的按鈕,這樣被存儲的值正確,但AlarmManager無法每分鐘撥打BatteryPost(只需每分鐘測試一次,時間會更長)。

我一直在看SO上的其他帖子,試圖找到類似的代碼,但實現它已變得棘手。

謝謝。

回答

1

我注意到的第一件事是你有它檢查600000毫秒。這不是60秒,而是600秒,換句話說,是10分鐘。這可能是問題嗎?

+0

也許,現在檢查... – sbrichards 2012-07-25 18:39:20

+0

不,沒有更新電池信息。 – sbrichards 2012-07-25 18:42:15

+0

剛剛錯過了一件事,現在就開始工作,但我會忽略這一點。謝謝! – sbrichards 2012-07-25 18:51:53

相關問題