0

我正在尝试使用AlarmManager来安排任务在预定义的时间运行。这AlarmManager是从我的应用程序初始化并在后台运行(运行良好),当在方法Broadcast内部接收到事件时,我调用一个服务,在其中我试图重新安排它来调用说“15 分钟后”;但由于某些原因它不起作用。它所做的一切就是停止.onReceive()context.startService(webServiceIntent);PendingIntentPendingIntent

以下是相关来源:

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

1 回答 1

0

问题 1:如果在您的通话SystemClock.elapsedRealtime()期间毫秒计时结束,可能已经过去了。setRepeating()确保您指定未来的时间。

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

startService()问题 3:许多设备在您的通话和之间睡着了onHandleIntent(),这就是我写WakefulIntentService.

于 2012-05-06T12:26:00.893 回答