在 android 4.4 中,从最近的任务中刷出应用程序会永久杀死应用程序及其服务.知道为什么吗? [英] In android 4.4, swiping app out of recent tasks permanently kills application with its service . Any idea why?

查看:29
本文介绍了在 android 4.4 中,从最近的任务中刷出应用程序会永久杀死应用程序及其服务.知道为什么吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Unlike previous versions, in 4.4, swiping app out of recent tasks permanently kills app along with its service(like force-stop) even though it's running background services. It shows 0 processes 1 service but service also doesn't work. Ideally it shouldn't kill background service, and it doesn't in versions prior to 4.3. Any idea why is it happening in 4.4?

推荐答案

明白了.它是 4.4 中的一个错误.我试过了,效果很好(虽然这是一个肮脏的锻炼).

Got it. Its a bug in 4.4. I tried this and it worked perfectly fine(its a dirty workout though).

只需覆盖此方法-:

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

这篇关于在 android 4.4 中,从最近的任务中刷出应用程序会永久杀死应用程序及其服务.知道为什么吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆