0

i have repeating service which runs every 5 minutes as long as the app is installed, but some times my service stops or gets killed, how do i make it fail safe and restart the service if it gets killed by the OS.

This is how im starting my repeating service.

    Intent serviceIntent = new Intent(context, SyncService.class);
    final AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    long interval = 60000; // 5 Mins - 1000*60; 1 sec=1000, 1min sec = 60*1000=60000
        final PendingIntent pending = PendingIntent.getService(context, 0, serviceIntent, 0);
    alarm.setRepeating(AlarmManager.RTC ,System.currentTimeMillis(), interval, pending);

and this is my service:

public class SyncService extends IntentService {

public SyncService() {
    super("SyncService"); 
    // TODO Auto-generated constructor stub
}

@Override
protected void onHandleIntent(Intent intent) {


    SyncToServer();


 }
 }
parvez rafi
  • 464
  • 4
  • 20
  • refer this answers :http://stackoverflow.com/questions/9696861/how-can-we-prevent-a-service-from-being-killed-by-os – santoXme Dec 07 '15 at 08:14

1 Answers1

2

IntentService always gets killed when work is done. If you want a long running service use the basic service instead: http://developer.android.com/reference/android/app/Service.html

and_dev
  • 3,723
  • 1
  • 20
  • 28