AlaramManager安排任务在预定义时间后运行



我正在尝试使用AlarmManager来安排任务在预定义的时间运行。这个AlarmManager是从我的应用程序初始化并在后台运行的(运行良好(,当在方法中收到Broadcast事件时onReceive()我使用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()通话期间毫秒滴答作响,SystemClock.elapsedRealtime()可能已经过去了。请确保指定将来的时间。

问题 #2:如果您在设备的当前时区13:35后运行此代码,则13:35可能是过去的。请确保指定将来的时间。

问题#3:该设备在您的startService()通话和onHandleIntent()之间都睡着了,这就是我写WakefulIntentService的原因。

最新更新