Android AlarmManager setRepeating不会以长间隔重复

前端之家收集整理的这篇文章主要介绍了Android AlarmManager setRepeating不会以长间隔重复前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经实现了AlarmManager每天唤醒一次手机以执行更新任务,更新小部件并发送通知(如果适用).

我正在使用setRepeating和ELAPSED_REALTIME_WAKEUP
第一次触发警报(SystemClock.elapsedRealtime()60000)
但它不会在86400000毫秒(24小时)之后触发.

对此真的很挣扎,我很高兴接受我做错了什么或者是否有更好的方法来实现我想做的事情.但是我认为我的代码看起来像人们似乎做的标准事情.

这几乎就像重复警报在所有情况下都没有做到它应该做的事情.如果我将间隔减少到10分钟它确实有效,我的警报就会触发,服务会一遍又一遍地运行.

我的应用程序的性质意味着每天更新一次是矫枉过正.
我需要找到一个现实可靠的解决方案.

谢谢你的时间和希望你能指出我正确的方向.

这是我的警报实施代码……

表现:

  1. <receiver android:name=".SystemChangeReceiver">
  2. <intent-filter>
  3. <action android:name="android.intent.action.BOOT_COMPLETED" />
  4. <action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
  5. </intent-filter>
  6. </receiver>
  7. <receiver android:name=".UpdateAlarmReceiver" />
  8. <service android:name=".UpdateService" />
  9. <receiver android:name=".WidgetProviderSmall" android:label="@string/widget_small_label">
  10. <intent-filter>
  11. <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
  12. </intent-filter>
  13. <Meta-data
  14. android:name="android.appwidget.provider"
  15. android:resource="@xml/appwidget_small" />
  16. </receiver>
  17. <receiver android:name=".WidgetProviderLarge" android:label="@string/widget_large_label">
  18. <intent-filter>
  19. <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
  20. </intent-filter>
  21. <Meta-data
  22. android:name="android.appwidget.provider"
  23. android:resource="@xml/appwidget_large" />
  24. </receiver>

SystemChangeReceiver侦听引导广播,检查是否需要设置警报,如果需要,则设置它.

SystemChangeReceiver:

  1. @Override
  2. public void onReceive(Context context,Intent intent) {
  3.  
  4. SharedPreferences prefs = context.getSharedPreferences(context.getString(R.string.prefs_name),0);
  5.  
  6. Boolean notifications = prefs.getBoolean("enable_updates",false);
  7. if(notifications == true) {
  8. Utils.setNotificationAlarm(context);
  9. }
  10. }

setNotificationAlarm方法,设置重复警报……

  1. public static void setNotificationAlarm(Context context) {
  2. AlarmManager alarmManager=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
  3.  
  4. Intent intent = new Intent(context,UpdateAlarmReceiver.class);
  5. PendingIntent pi = PendingIntent.getBroadcast(context,intent,0);
  6.  
  7. alarmManager.setRepeating(
  8. AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime()+60000,86400000,pi);
  9. }

当警报触发我的接收器UpdateAlarmReceiver决定做什么并使用WakefulIntentService运行我的后台更新过程时,该服务的处理程序然后更新小部件并根据需要发送通知

UpdateAlarmReceiver:

  1. public void onReceive(Context context,Intent intent) {
  2. WakefulIntentService.sendWakefulWork(context,UpdateService.class);
  3. }

解决方法

没有什么明显的错误.

在很长一段时间内,我建议使用RTC_WAKEUP,这样你就可以安排它在半夜或者用户可选择的时间发生,而不是从半随机起点开始每24小时发生一次. . RTC_WAKEUP可能会给你更好的结果.

也:

>向UpdateAlarmReceiver的onReceive()添加一个日志语句,以确认您是否正在获得控制权,或者问题是否是(使用WakefulIntentService).>尝试稳步增加你的时间.因为你说10分钟的工作,尝试一小时,然后四个小时等,并尝试在它崩溃时有所了解.>“我实际上正在使用AutoKiller内存优化器它没有白名单这可能是问题?我的应用程序仍在运行,但根据进程列表.” – 我会禁用所有任务杀手,只是为了确保他们没有干扰.

猜你在找的Android相关文章