Search code examples
androidservicealarmmanager

Android : restart the service


My application have a Service that every X minutes do same action on the database than it stopSelf() and into onDestroy method I have palced this code for restart the service after same time:

@Override
public void onDestroy() {
    super.onDestroy();
    Intent intent_service = new Intent(context,vampireService.class);
    PendingIntent pintent = PendingIntent.getService(context, 0, intent_service,0);
    AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarm.set(AlarmManager.ELAPSED_REALTIME,SystemClock.elapsedRealtime()+ 4500000, pintent);
}

But I don't understand why if my phone go in sleep mode the service not restart ! Appears that the count time of AlarmManager to start when I power back up the display....it's possibile ? If yes, how can I resolve this problem ?

Thanks


Solution

  • From the documentation for ELAPSED_REALTIME...

    This alarm does not wake the device up; if it goes off while the device is asleep, it will not be delivered until the next time the device wakes up.

    Try using ELAPSED_REALTIME_WAKEUP to see if that helps (not sure if it will work for a service however).