从最近的托盘切换后为什么我的Android应用程序强制停止

时间:2016-10-21 01:28:50

标签: android android-activity service process background

我的Android应用程序在第二次尝试时强制停止我清除最近的托盘。在我的第一次它没有被强制停止..可能是什么原因??
我的服务类

public class TheService extends Service {
 @Override
public void onCreate() {
    super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.v("myapp","service started");
    return START_STICKY;
}
@Override
public void onTaskRemoved(Intent intent)
{
    Log.v("HAHAHA","TASK IS REMOVED");
    stopService(new Intent(getApplicationContext(),this.getClass()));
        Intent restartService = new Intent(getApplicationContext(),
                this.getClass());
        restartService.setPackage(getPackageName());
        Random generator = new Random();
        PendingIntent restartServicePI = PendingIntent.getService(
                getApplicationContext(), generator.nextInt(), restartService,
                PendingIntent.FLAG_ONE_SHOT);
        AlarmManager alarmService = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
        alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() +1000, restartServicePI);
    super.onTaskRemoved(intent);
}
@Override
public void onDestroy() {
    Log.v("HAHAHA","APP KILLED");
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

首次尝试LogCat

10-24 06:44:11.742 6958-6958/miniproject.gazastreet V/HAHAHA: TASK IS REMOVED
10-24 06:44:11.752 6958-6958/miniproject.gazastreet V/HAHAHA: APP KILLED
10-24 06:44:12.752 6958-7148/miniproject.gazastreet V/myapp: service started

第二次尝试LogCat

10-24 06:46:48.508 6958-6958/miniproject.gazastreet V/HAHAHA: TASK IS REMOVED
10-24 06:46:48.518 6958-6958/miniproject.gazastreet V/HAHAHA: APP KILLED

为什么每次都这样? 提前谢谢。

1 个答案:

答案 0 :(得分:0)

在Android 5.0及更高版本中,当应用程序不在forground中时,服务将被销毁。