Android AlarmManager setRepeating不会以长间隔重复

Rob*_*Rob 7 android alarmmanager

我已经实现了AlarmManager每天唤醒一次手机以执行更新任务,更新小部件并发送通知(如果适用).

我正在使用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>
Run Code Online (Sandbox Code Playgroud)

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

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

UpdateAlarmReceiver:

public void onReceive(Context context, Intent intent) {
    WakefulIntentService.sendWakefulWork(context, UpdateService.class);
}
Run Code Online (Sandbox Code Playgroud)

Foa*_*Guy 1

您是否尝试过将其设置为不重复。然后,当它响起时,您将下一次警报设置为 24 小时后吗?这就像重复警报一样,但可能会避免您遇到的一些问题。