Java AlamManager计划任务在预定义时间后运行
我正在尝试使用Java AlamManager计划任务在预定义时间后运行,java,android,broadcastreceiver,alarmmanager,android-pendingintent,Java,Android,Broadcastreceiver,Alarmmanager,Android Pendingintent,我正在尝试使用AlarmManager计划在预定义时间运行任务。 当在onReceive()中接收到Broadcast事件时,此AlarmManager从我的应用程序初始化并在后台运行(运行良好)方法使用context.startService(WebServiceContent)调用服务里面我正试图重新安排这个挂起的内容打电话说“15分钟后”;但由于某些原因,它不起作用。它所做的一切都会停止挂起内容 以下是有关资料来源: //Initiliazing AlarmManager from my
AlarmManager
计划在预定义时间运行任务。
当在onReceive()中接收到Broadcast
事件时,此AlarmManager
从我的应用程序初始化并在后台运行(运行良好)
方法使用context.startService(WebServiceContent)调用服务代码>里面我正试图重新安排这个挂起的内容
打电话说“15分钟后”;但由于某些原因,它不起作用。它所做的一切都会停止挂起内容
以下是有关资料来源:
//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:SystemClock.elapsedRealtime()
如果在setRepeating()
调用期间毫秒滴答滴答地过去了,那么它可能已经过去了。请确保指定将来的时间
问题#2:13:35
如果您在设备当前时区的13:35
之后运行此代码,则可能是过去的问题。请确保指定将来的时间
问题#3:很多设备在您的startService()
调用和onHandleIntent()
之间睡着了,这就是我为什么要写的原因。是的,我让它运行了,实际上我用AlarmManager.RTC替换了AlarmManager.Appeased\u REALTIME\u WAKEUP。但现在问题出在wakefulIntentService的这一行PendingEvent.getBroadcast中(this,0,GetLocationPollerinent(),PendingEvent.FLAG_UPDATE_CURRENT);创建新的PendingEvent而不是更新current@Waqas:然后,getLocationPollerIntent()
返回一个不同的意图,一个不同于其他的意图。嗯,您知道getApplicationContext()是否从可见活动中调用和从此wakeful intentservice调用时返回相同/不同的值?如果它返回不同的值,那么你知道如何使这两个值的意图相同吗?@Waqas:这不是你的问题。您的操作不同,组件不同,数据(Uri
)不同,类别不同,或者MIME类型不同。请参见Intent
上的filterEquals()
实现: