I know using setExact for repeating tasks is a bad behavior, but it is a requirement for a mission.
I work on a Samsung Galaxy Note 8 running KitKat and I am using the setExact
method from the AlarmManager
in order to schedule an IntentService
at a specific time. I had to abandon setRepeating
as the documentation said that from SDK 19, it will be inexact.
When my BroadcastReceiver
gets fired up, I start my service and then I re-schedule it with another call to setExact
, thus mimicking the old setRepeating
from pre-19.
This happens every 30 minutes.
Looking at my logs, I can see that my service was triggered from 5PM (when I started the program) to 4AM last night with the 30 minutes interval as expected and then ... nothing. As of writing this post, it still won't be triggered.
I also closed the application since the beginning in order to see if works when the application is not being used. But in Android Studio, the monitor does show that the process is alive.
This is the code that will schedule the service the first time:
final Intent activityDataFetcherIntent = new Intent(appContext, ServiceReceiver.class);
final PendingIntent pendingIntent = PendingIntent.getBroadcast(appContext, 4334, activityDataFetcherIntent, 0);
final AlarmManager manager = (AlarmManager) appContext.getSystemService(Context.ALARM_SERVICE);
manager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(30), pendingIntent);
And finally this is the code from the BroadcastReceiver:
public final static class ServiceReceiver extends BroadcastReceiver {
@Override
public final void onReceive(final Context context, final Intent intent) {
final Intent service = new Intent(context, ActivityFetcherService.class);
context.startService(service);
// Rescheduling for 30 minutes later
final Intent activityDataFetcherIntent = new Intent(context, getClass());
final PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 4334, activityDataFetcherIntent, 0);
final AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
manager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(30), pendingIntent);
}
}
This also means that I reschedule the service while it's still running. But all in all the service only runs for 10-15 seconds at most.
Obviously both BroadcastReceiver and Service are declared in the Manifest or else they woud not be fire at all.
What am I doing wrong ?