gpt4 book ai didi

android - Android:退出应用后,AlarmManager无法启动应用

转载 作者:行者123 更新时间:2023-12-02 13:40:25 25 4
gpt4 key购买 nike

我试图使一个应用程序从服务器发送的命令重新启动,然后在互联网上搜索,我发现做到这一点的最佳方法是使用AlarmManager,因此它会在某个时候启动该应用程序。
因此,我到目前为止所做的代码如下所示:

val intent = Intent(this, StartActivity::class.java)
val wait = TimeUnit.SECONDS.toMillis(3)
val intentId = 123

intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.putExtra(EXTRA_RESTART_ID, this.restartId)
intent.putExtra(EXTRA_COMMAND_NAME, this.restartCommandName)

val pendingIntent = PendingIntent.getActivity(this.applicationContext, intentId, intent, PendingIntent.FLAG_ONE_SHOT)
val manager: AlarmManager = this.getSystemService(Context.ALARM_SERVICE) as AlarmManager

// Set alarm manager to start after 3s
manager.set(AlarmManager.RTC, System.currentTimeMillis() + wait, pendingIntent)

// Kill application
this.finish()
exitProcess(2)
这样做时,我注意到该应用程序无法始终如一地重新启动..有时它不起作用,我也不知道为什么不这样做,我尝试更改等待时间以及意图ID和退出代码,但它仍然是不一致的。
另外,使用 adb shell dumpsys alarm在shell中检查警报系统不会显示应用程序添加的警报。
有时应用无法再次启动的原因可能是什么?
注意:手机正在Android 9上运行

最佳答案

What could be the cause of the app not starting again sometimes?


在android〜6及更高版本中,任何在后台运行的后台服务,作业,任务和其他身份都不会在实时状态下停留大约1分钟以上,并且安排在接下来的15分钟内。
这些限制和限制是由于电源管理和安全策略增加了用户的友好体验。
在出现这些限制之前,应用程序会有意或无意地破坏您的能力。
对于您的特定问题,我强烈建议您使用Google可信第三方软件包或Google信任的任何其他身份。

关于android - Android:退出应用后,AlarmManager无法启动应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64072001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com