Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restart a Service after getting Killed by apps like "Advanced Task Killer"?

I have a public class that 'extends Service' and this service is launched from an activity using startService(...). But after I use Advanced Task Killer, the service is killed and never restarted again.

I noticed that some apps like the Facebook Messenger Android App restart automatically even after killing them from Advanced Task Killer ... how are the facebook/twitter apps doing it??

enter image description here

like image 771
user1406716 Avatar asked Dec 13 '13 00:12

user1406716


3 Answers

If you want to restart your service automatically after being killed by another process, you can use following constants in your service,

@Override
public int onStartCommand(Intent intent, int flags, int startId) { 
    return START_STICKY;
}

You can find more information on START_STICKY & START_NON_STICKY at,

START_STICKY and START_NOT_STICKY

Also please provide your code for more specific answer.

like image 159
GDroid Avatar answered Nov 09 '22 23:11

GDroid


Android system, or the user, may terminate a service at any time. For this reason if you want to ensure something is always running, you can schedule a periodic restart by means of AlarmManager class. The following code demonstrates how to do this.

Calendar cal = Calendar.getInstance();

Intent intent = new Intent(this, MyService.class);
PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);

AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
// Start every minute
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 60*1000, pintent); 

You can run this code when the user starts the app (i.e. in the oncreate of the first activity) but you have to check if it is already done, so probably it will be better if you create a broadcast receiver, than launches this code on system reboot.

like image 12
Carlos Robles Avatar answered Nov 09 '22 23:11

Carlos Robles


Make onStartCommand() in your service return START_STICKY

  /**
     * Constant to return from {@link #onStartCommand}: if this service's
     * process is killed while it is started (after returning from
     * {@link #onStartCommand}), then leave it in the started state but
     * don't retain this delivered intent.  Later the system will try to
     * re-create the service.  Because it is in the started state, it will
     * guarantee to call {@link #onStartCommand} after creating the new
     * service instance; if there are not any pending start commands to be
     * delivered to the service, it will be called with a null intent
     * object, so you must take care to check for this.
     * 
     * <p>This mode makes sense for things that will be explicitly started
     * and stopped to run for arbitrary periods of time, such as a service
     * performing background music playback.
     */
    public static final int START_STICKY = 1;
like image 2
Martin Marconcini Avatar answered Nov 09 '22 22:11

Martin Marconcini