Reputation: 353
I want to restart my app when it is cleared from the running apps list. How do i do so? this is the incomplete code for my service
public class MyService extends Service {
Handler handler = new Handler();
final double specifiedlimit=0.001;
int f = 0;
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(final Intent intent, int flags, int startId) {
final long intervalTime = 10000; // 5 mins
handler.postDelayed(new Runnable() {
@Override
public void run() {
gpstracker();
handler.postDelayed(this, intervalTime);
}
}, intervalTime);
return START_STICKY;
}
Upvotes: 0
Views: 53
Reputation: 1735
Implement onTaskRemoved()
in your service.
@Override
public void onTaskRemoved(Intent rootIntent) {
Log.e("Service", "On task removed");
Intent restartService = new Intent(getApplicationContext(), this.getClass());
restartService.setPackage(getPackageName());
PendingIntent restartServicePI = PendingIntent.getService(
this, 1, restartService,
PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 1000, restartServicePI);
}
Upvotes: 1