在Android 4.4中,从最近的任务中滑动应用程序会永久性地杀死应用程序及其服务.知道为什么吗?

Vip*_*l J 15 service android android-4.4-kitkat

与以前的版本不同,在4.4中,从最近的任务中滑动应用程序会永久杀死应用程序及其服务(如强制停止),即使它正在运行后台服务.它显示0个进程1服务,但服务也不起作用.理想情况下,它不应该杀死后台服务,它不会在4.3之前的版本中.知道为什么会发生在4.4?

Vip*_*l J 26

得到它了.这是4.4中的一个错误.我试过这个并且它工作得非常好(虽然这是一个肮脏的锻炼).

只需覆盖此方法 - :

public void onTaskRemoved(Intent rootIntent) {
    Log.e("FLAGX : ", ServiceInfo.FLAG_STOP_WITH_TASK + "");
    Intent restartServiceIntent = new Intent(getApplicationContext(),
            this.getClass());
    restartServiceIntent.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);
}
Run Code Online (Sandbox Code Playgroud)

  • 我正在使用它,因为你刷卡的那一刻,android杀死所有东西,包括你的服务,然后没有办法重新启动你的服务.通过设置闹钟,我确保重新启动服务. (5认同)