0
我試圖使用AlarmManager
計劃任務在預定義時間運行。 這個AlarmManager
從我的應用程序初始化並在後臺運行(運行良好),當收到Broadcast
事件時onReceive()
方法我打電話服務使用context.startService(webServiceIntent);
其中我試圖重新安排這個PendingIntent
調用說'15分鐘後' ;但由於某些原因,它不起作用。它所做的就是停止PendingIntent
。AlaramManager將計劃任務在預定義時間後運行
下面是相關人士透露:
//Initiliazing AlarmManager from my app
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getBroadcast(this, 0, getLocationPollerIntent(), 0);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), Constant.LOCATION_POLLING_PERIOD, pendingIntent);
//onReceive Method of BroadcastReceiver
@Override
public void onReceive(Context context, Intent intent) {
Intent webServiceIntent = new Intent(context, LocationNotificationService.class);
webServiceIntent.putExtra("UPDATED_LOCATION", loc);
context.startService(webServiceIntent);
}
//Inside LocationNotificationService to reschedule the PendingIntent - This PendingIntent never get called
protected void onHandleIntent(Intent intent) {
c.set(Calendar.HOUR_OF_DAY, 13);
c.set(Calendar.MINUTE, 35);
c.set(Calendar.SECOND, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, getLocationPollerIntent(), PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, c.getTimeInMillis(), Constant.LOCATION_POLLING_PERIOD, pendingIntent);
}
是的,我得到它運行實際上我用AlarmManager.RTC替換AlarmManager.ELAPSED_REALTIME_WAKEUP。但現在問題是在wakefulIntentService這一行PendingIntent.getBroadcast裏面(this,0,getLocationPollerIntent(),PendingIntent.FLAG_UPDATE_CURRENT);創建新的PendingIntent而不是更新當前 – Waqas
@Waqas:然後'getLocationPollerIntent()'返回一個不同的'Intent',一個不同於extras的。 – CommonsWare
嗯,你知道如果getApplicationContext()在可見活動中調用並從這個覺醒的intentservice調用時返回相同/不同的值嗎?如果它返回不同的值之間,那麼任何想法如何使這兩個意圖相同? – Waqas