我的闹钟管理器在后台不起作用。

huangapple go评论80阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年8月2日 02:13:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63208559.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定