Should i call onCreate method in onDestroy method in android service?

前端 未结 1 410
南旧
南旧 2021-01-25 17:03

I want to startservice again when it was destroyed.

@Override
    public void onDestroy() {
        super.onDestroy();
        onCreate();
    }
相关标签:
1条回答
  • 2021-01-25 17:53

    You should not do this . This is not going to do anything. To restart a service you should restart it on onTaskRemove() like below.

    @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);
    }
    
    0 讨论(0)
提交回复
热议问题