我想在它被销毁时再次启动服务。
@Override
public void onDestroy() {
super.onDestroy();
onCreate();
}
答案 0 :(得分:2)
你不应该这样做。这不会做任何事情。要重新启动服务,您应该在 onTaskRemove()上重新启动它,如下所示。
@Override
public void onTaskRemoved(Intent rootIntent) {
Intent restartService = new Intent(getApplicationContext(),
this.getClass());
restartService.setPackage(getPackageName());
PendingIntent restartServicePI = PendingIntent.getService(
getApplicationContext(), 1, restartService,
PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 1000, restartServicePI);
}