服务onTaskRemoved()如何再次启动服务

时间:2017-01-09 10:02:01

标签: android service broadcastreceiver alarmmanager

这个问题问了这么多次,但我没有得到任何解决方案如何开始 在onTaskRemoved()

中再次服务

我尝试了以下方法,但它不适合我。

Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
    PendingIntent restartServicePendingIntent = PendingIntent.getService(
            getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
    AlarmManager alarmService = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmService.set(alarmService.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 1000,restartServicePendingIntent);

1 个答案:

答案 0 :(得分:0)

     @Override
    public void onTaskRemoved(Intent rootIntent) {
        Log.e("FLAGX : ", ServiceInfo.FLAG_STOP_WITH_TASK + "");

        Intent restartServiceIntent = new Intent(getApplicationContext(),
                this.getClass());
        restartServiceIntent.setPackage(getPackageName());

        Intent intent = new Intent(getApplicationContext(), this.getClass());

        intent.setPackage(getPackageName());

        PendingIntent restartServicePendingIntent = PendingIntent.getService(
                getApplicationContext(), 1, restartServiceIntent,
                PendingIntent.FLAG_ONE_SHOT);
        AlarmManager alarmService = (AlarmManager) getApplicationContext()
                .getSystemService(Context.ALARM_SERVICE);
        alarmService.set(AlarmManager.ELAPSED_REALTIME,
                SystemClock.elapsedRealtime() + 1000,
                restartServicePendingIntent);

        super.onTaskRemoved(rootIntent);
    }

其实我不知道它是如何工作的,但是这段代码可以帮助你......