从正在运行的应用列表中清除时,Android应用会关闭。如何重新启动

时间:2016-06-09 10:20:22

标签: android

我想从正在运行的应用列表中清除应用时重启我的应用。我怎么做的?这是我服务的不完整代码

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;

}

1 个答案:

答案 0 :(得分:1)

在您的服务中实施onTaskRemoved()

 @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);
}