我们如何使用 Android 9 及更高版本在崩溃时自动重启应用程序

问题描述

我正在尝试在崩溃几秒钟后自动重新启动应用程序。当应用程序崩溃时,它也会关闭后台服务。我使用了以下适用于 android 较低版本(android 8 或更低版本)的 solution。有没有办法在崩溃的情况下从后台启动服务? (我的应用目标版本是 9 及更高版本)

作为参考,我使用的这段代码对 android 9 没有用:

Thread.setDefaultUncaughtExceptionHandler { thread,e ->
        handleUncaughtException(thread,e)
        val intent = Intent(this,MessageService::class.java)
        intent.addFlags(
            Intent.FLAG_ACTIVITY_CLEAR_TOP
                    or Intent.FLAG_ACTIVITY_CLEAR_TASK
                    or Intent.FLAG_ACTIVITY_NEW_TASK
        )
        val pendingIntent = PendingIntent.getService(
            this,intent,0
        )
        val mgr = applicationContext.getSystemService(ALARM_SERVICE) as AlarmManager
        mgr[AlarmManager.RTC,System.currentTimeMillis() + 5000] = pendingIntent
        this.stopSelf()
        exitProcess(2)
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)