簡體   English   中英

AlaramManager計划任務在預定義時間后運行

[英]AlaramManager to schedule task to run after predefined time

我正在嘗試使用AlarmManager安排任務在預定義的時間運行。 當從onReceive()方法中接收到Broadcast事件時,我的應用程序初始化了此AlarmManager並在后台運行(運行良好),我使用context.startService(webServiceIntent);調用服務context.startService(webServiceIntent); 在其中,我試圖重新安排此PendingIntent的通話時間,例如“ 15分鍾后”; 但由於某些原因,它無法正常工作。 它所做的一切都會停止PendingIntent

以下是相關來源:

//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);
}

問題#1:如果在setRepeating()調用期間經過毫秒數setRepeating() ,則SystemClock.elapsedRealtime()可能已經過去。 確保指定將來的時間。

問題13:35可能是過去的時間,如果您在設備當前時區的13:35之后運行此代碼。 確保指定將來的時間。

問題#3:許多設備在您的startService()調用和onHandleIntent()之間進入了睡眠狀態,這就是我編寫WakefulIntentService

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM