我想在它被销毁时再次启动服务。
@Override
public void onDestroy() {
super.onDestroy();
onCreate();
}
最佳答案
你不应该这样做。这不会做任何事情。要重新启动服务,您应该像下面那样在 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);
}
关于android - 我应该在android服务的onDestroy方法中调用onCreate方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47587720/