Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid AlarmManager setRepeating doesn't repeat with long interval
    primarykey
    data
    text
    <p>I've implemented AlarmManager to wake the phone once a day to perform an update task, update widgets and send notifications if applicable.</p> <p>I'm using <code>setRepeating</code> and <code>ELAPSED_REALTIME_WAKEUP</code> The alarm is triggered the first time (<code>SystemClock.elapsedRealtime()+60000</code>) but it doesn't get triggered <code>86400000</code> milliseconds (24 hours) later.</p> <p>Really struggling with this, I'm happy to accept im doing something wrong or if there are better ways to achieve what I'm trying to do. However I think my code looks like the standard thing people seem to do.</p> <p>It's almost like the repeating alarm doesn't do what it says it should in all cases. If I reduce the interval to say 10 minutes it does work, my alarm triggers and the service is run over and over.</p> <p>The nature of my app means updating more than once a day is overkill. I need to find a realistic reliable solution to this.</p> <p>Thanks for your time &amp; hope you can point me in the right direction.</p> <p>Here's my alarm implementation code...</p> <p>Manifest:</p> <pre><code>&lt;receiver android:name=".SystemChangeReceiver"&gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.BOOT_COMPLETED" /&gt; &lt;action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" /&gt; &lt;/intent-filter&gt; &lt;/receiver&gt; &lt;receiver android:name=".UpdateAlarmReceiver" /&gt; &lt;service android:name=".UpdateService" /&gt; &lt;receiver android:name=".WidgetProviderSmall" android:label="@string/widget_small_label"&gt; &lt;intent-filter&gt; &lt;action android:name="android.appwidget.action.APPWIDGET_UPDATE" /&gt; &lt;/intent-filter&gt; &lt;meta-data android:name="android.appwidget.provider" android:resource="@xml/appwidget_small" /&gt; &lt;/receiver&gt; &lt;receiver android:name=".WidgetProviderLarge" android:label="@string/widget_large_label"&gt; &lt;intent-filter&gt; &lt;action android:name="android.appwidget.action.APPWIDGET_UPDATE" /&gt; &lt;/intent-filter&gt; &lt;meta-data android:name="android.appwidget.provider" android:resource="@xml/appwidget_large" /&gt; &lt;/receiver&gt; </code></pre> <p><code>SystemChangeReceiver</code> listens for the boot broadcast, checks if an alarm needs to be set, if it does, it sets it.</p> <p><code>SystemChangeReceiver</code>:</p> <pre><code>@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); } } </code></pre> <p>The setNotificationAlarm method, sets up the repeating alarm...</p> <pre><code>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); } </code></pre> <p>When the alarm triggers my receiver <code>UpdateAlarmReceiver</code> decides what to do and using <code>WakefulIntentService</code> runs my background update process, the handler for the service then updates widgets and sends notifications as required</p> <p><code>UpdateAlarmReceiver</code>:</p> <pre><code>public void onReceive(Context context, Intent intent) { WakefulIntentService.sendWakefulWork(context, UpdateService.class); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload