1

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 ?

Mackovich
  • 3,319
  • 6
  • 35
  • 73
  • 1
    is this your case? please check this accepted answer at http://stackoverflow.com/a/20745776/4862126 – Er. Kaushik Kajavadara Apr 20 '17 at 07:22
  • Thanks for your comment. Could it be because I use the same ID all the time ? Should I generate a random id each time in my BroadcastReceiver ? – Mackovich Apr 20 '17 at 07:25
  • 1
    Yes, you can use incremental int preference value to avoid getting same ID for multiple instances that you may get in random id generation. – Er. Kaushik Kajavadara Apr 20 '17 at 07:28
  • Got it, I will try it right away and see if it works in the long run. Many thanks for the proposition :) – Mackovich Apr 20 '17 at 07:41
  • @Er.KaushikKajavadara well, it seems to work just fine for the whole day. I'll monitor the logs overnight to see if it holds. Thanks for the tip! – Mackovich Apr 20 '17 at 16:36

0 Answers0