英文:
My Alarm Manager does not work in background
问题
我已为我的应用程序准备好了闹钟管理器。我需要每小时运行它并检查数据是否已更改。
我已经设置了闹钟管理器如下:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR, 1);
android.app.AlarmManager alarmMgr = (android.app.AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(mContext, AnalysisNotificationReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, ALARM_ID, intent, 0);
if (Calendar.getInstance().after(cal)) {
cal.add(Calendar.DAY_OF_MONTH, 1);
}
alarmMgr.setRepeating(android.app.AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 60 * 60 * 1000, pendingIntent);
应该每60分钟运行一次,我不确定我是否设置正确,但在关闭应用程序时不起作用。
有人有想法吗?
谢谢
英文:
I prepared for my application alarm manager. I need run this every hour and check if data is changed.
I have set the alarm manager like this:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR, 1);
android.app.AlarmManager alarmMgr = (android.app.AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(mContext, AnalysisNotificationReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, ALARM_ID, intent, 0);
if (Calendar.getInstance().after(cal)) {
cal.add(Calendar.DAY_OF_MONTH, 1);
}
alarmMgr.setRepeating(android.app.AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 60 * 60 * 1000, pendingIntent);
It's should work in every 60 minutes I'm not sure if I set correctly but doesn't work when app close.
Anyone have a idea?
Thanks
答案1
得分: 0
你可以尝试这样做,这对我有用:
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent scheduleServiceExecuterIntent = new Intent(this, ScheduledServiceExecuter.class);
scheduleServiceExecuterIntent.putExtra("state", "Main");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, request_code, intent, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, AlarmManager.INTERVAL_HOUR, pendingIntent);
请注意,我已经翻译了你的代码部分。
英文:
You can try this, it worked for me
AlarmManager alarmManager= (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent scheduleServiceExecuterIntent = new Intent(this, ScheduledServiceExecuter.class);
scheduleServiceExecuterIntent.putExtra("state", "Main");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, request_code, intent, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, AlarmManager.INTERVAL_HOUR, pendingIntent);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论