我的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
为什么每次都这样? 提前谢谢。
答案 0 :(得分:0)
在Android 5.0及更高版本中,当应用程序不在forground中时,服务将被销毁。