Android AlarmManager setRepeating不会以长间隔重复

我已经实现了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>

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

解决方法

没有什么明显的错误.

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

也:

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

相关文章

Android 如何解决dialog弹出时无法捕捉Activity的back事件 在...
Android实现自定义带文字和图片的Button 在Android开发中经常...
Android 关于长按back键退出应用程序的实现最近在做一个Andr...
android自带的时间选择器只能精确到分,但是对于某些应用要求...