销毁后,我想再次启动。

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

10-06 02:32