我已经实现了AlarmManager来每天唤醒一次电话以执行更新任务,更新小部件并发送通知(如果适用)。Android AlarmManager setRepeating不会长时间间隔重复
我使用setRepeating
和ELAPSED_REALTIME_WAKEUP
报警被触发第一次(SystemClock.elapsedRealtime()+60000
) ,但它不会86400000
毫秒(24小时)后触发。
真的很难与此,我很高兴接受我做错了什么,或者如果有更好的方法来实现我想要做的事情。不过,我认为我的代码看起来像人们似乎做的标准事情。
这几乎就像重复报警没有做它说的应该在所有情况下。如果我将时间间隔缩短到10分钟,它就会起作用,我的警报会触发并且服务会一遍又一遍地运行。
我的应用程序的本质意味着每天更新不止一次是矫枉过正。 我需要找到一个现实可靠的解决方案。
感谢您的时间&希望您能指出我的正确方向。
这里是我报警的实现代码...
清单:
<receiver android:name=".SystemChangeReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
</intent-filter>
</receiver>
<receiver android:name=".UpdateAlarmReceiver" />
<service android:name=".UpdateService" />
<receiver android:name=".WidgetProviderSmall" android:label="@string/widget_small_label">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/appwidget_small" />
</receiver>
<receiver android:name=".WidgetProviderLarge" android:label="@string/widget_large_label">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/appwidget_large" />
</receiver>
SystemChangeReceiver
侦听启动广播,如果报警需要检查设置,如果这样做,将其设置。
SystemChangeReceiver
:
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences prefs = context.getSharedPreferences(context.getString(R.string.prefs_name), 0);
Boolean notifications = prefs.getBoolean("enable_updates", false);
if(notifications == true) {
Utils.setNotificationAlarm(context);
}
}
的setNotificationAlarm方法,设置重复报警......
public static void setNotificationAlarm(Context context) {
AlarmManager alarmManager=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, UpdateAlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
alarmManager.setRepeating(
AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime()+60000,
86400000,
pi);
}
当警报触发我的接收机UpdateAlarmReceiver
决定做什么和使用WakefulIntentService
运行我的后台更新过程,服务处理程序然后更新小部件并根据需要发送通知
UpdateAlarmReceiver
:
public void onReceive(Context context, Intent intent) {
WakefulIntentService.sendWakefulWork(context, UpdateService.class);
}
可以解决所有这些问题的快速问题:你使用任务杀手吗?你的应用在白名单上? –
我实际上使用AutoKiller Memory Optimizer,它没有白名单,这可能是问题?根据进程列表,我的应用程序仍在运行。 – Rob