android - 7.0及以上版本应用被杀后重启服务

标签 android service broadcastreceiver

在 Android 6.0 之前,每当我终止我的应用程序时,我的服务都会自动重启,但是当我在 7.1.1 中测试它时,我的服务不会重启,它会停止。

我试过像这样在 onDestroy Activity 中重启服务,

@Override
    protected void onDestroy() {
        super.onDestroy();
        Util.printMessage("onDestroy.....");
        Intent locationIntent = new Intent(getApplicationContext(), SupportService.class);
        startService(locationIntent);
        Util.printMessage("running...?..." + isMyServiceRunning(SupportService.class));

    }

但是这个解决方案没有用,任何人都可以建议是什么问题或如何再次启动服务,对于启动更改接收器,我的广播在设备重新启动时没有收到调用。

最佳答案

onStartCommand() 的返回类型更改为 START_STICKY。如果服务被系统破坏,它将自动启动...有关 START_STICKY 的更多信息 Click

关于android - 7.0及以上版本应用被杀后重启服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51023839/

相关文章:

android - 从代码注册的广播接收器

java - onclick 监听器的 Android 空指针异常

android - 添加 fragment 作为 ListView header

android - 如何检测用户在电池优化中从 "Not optimized"列表中删除了我的应用程序?

javascript停止特定服务

android - ping 时 sleep Android 设备唤醒

android - BOOT_COMPLETED 无法正常工作

java - android中字符串的比较

java - android 中 RxJava2 中的 subscribe() 和 subscribeWith() 有什么区别?

python - 使用 pandas 从 xml 获取数据