0

I have been trying to schedule an Android service to run repeatedly using the AlarmManager class, but the service stops being called after a couple of days. Originally I had been using the below code to try and achieve this:

Intent alarmIntent = new Intent(currentActivity.getApplicationContext(), AlarmReceiver.class);
PendingIntent pendingAlarmIntent = PendingIntent.getBroadcast(currentActivity.getApplicationContext(),
            AppManager.ALARM_PENDING_INTENT_REQUEST_CODE,
            alarmIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);

AlarmManager alarmManager = (AlarmManager)currentActivity.getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
            System.currentTimeMillis(),
            interval,
            pendingAlarmIntent);

with the receiver:

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context _context, Intent _intent){
        Intent serviceIntent = new Intent(_context.getApplicationContext(), PostLocationService.class);
        _context.getApplicationContext().startService(serviceIntent);
    }
}

This worked for a few days and then suddenly stopped. So I changed the code to initially broadcast to the receiver once and then re broadcast to the receiver each time the Service is run. I did this with the following code:

Intent alarmIntent = new Intent(currentActivity.getApplicationContext(),     AlarmReceiver.class);
PendingIntent pendingAlarmIntent = PendingIntent.getBroadcast(currentActivity.getApplicationContext(),
            AppManager.ALARM_PENDING_INTENT_REQUEST_CODE,
            alarmIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);

AlarmManager alarmManager = (AlarmManager)currentActivity.getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,
            interval,
            pendingAlarmIntent);

Unfortunately this has the same result; the Service stops being executed after a couple of days.

I have also registered a receiver for when the device boots to re initiate the AlarmManager call which is the same code as above.

Am I missing something? Is there a better approach than either of the above methods for this type of constant repetitive execution?

user2145312
  • 754
  • 2
  • 7
  • 27
  • Your service must be crashing. Make it log to a file, or to your web server, when it starts and stops, so you can find out if it is crashing. Also, if the phone is rebooting alarms are lost, you need to register a BroadcastReceiver to catch {android.intent.action.BOOT_COMPLETED}, for that you need to edit the app manifest file. https://stackoverflow.com/questions/12034357/does-alarm-manager-persist-even-after-reboot – Hatoru Hansou Aug 28 '17 at 17:25

1 Answers1

0

According to the developer site:

Note: Beginning with API 19 (KITKAT) alarm delivery is inexact: the OS will shift alarms in order to minimize wakeups and battery use. There are new APIs to support applications which need strict delivery guarantees; see setWindow(int, long, long, PendingIntent) and setExact(int, long, PendingIntent). Applications whose targetSdkVersion is earlier than API 19 will continue to see the previous behavior in which all alarms are delivered exactly when requested Blockquote

see more

hopes it will help

Du.Fantasy
  • 445
  • 2
  • 7
  • AlarmManager.set is still OK for most cases. When you use set(), you are telling the alarm manager that you want that action to happen at around that time, but that it is OK that the OS takes decisions to optimize battery life or to avoid too many alarms triggering too closely. – Hatoru Hansou Aug 28 '17 at 17:17