簡體   English   中英

如何以編程方式重啟 android 應用程序

[英]How to restart android app programmatically

我想通過待定意圖重新啟動我的應用程序。 下面的代碼不起作用。

val intent = Intent(this, Activity::class.java).apply {
     flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
}
val pendingIntentId = 1
val pendingIntent = PendingIntent.getActivity(this, pendingIntentId, intent, PendingIntent.FLAG_CANCEL_CURRENT)
val mgr = getSystemService(Context.ALARM_SERVICE) as AlarmManager
val timeToStart = System.currentTimeMillis() + 1000L
mgr.set(AlarmManager.RTC, timeToStart, pendingIntent)
exitProcess(0)

目標版本為 31,因此使用PendingIntent.FLAG_MUTABLE更新的待定意圖仍然無效。 我搜索了許多與此相關的鏈接,但沒有運氣。

以編程方式重啟 Android 應用程序

強制應用程序在第一次活動時重新啟動

https://www.folkstalk.com/tech/restart-application-programmatically-android-with-code-examples/#:~:text=How%20do%20I%20programmatically%20restart,finishes%20and%20automatically%20relaunches% 20us.%20%7D

2022 年 11 月,當目標版本為 31 且最低 sdk 版本為 29 時,上述未決意圖代碼不會重新啟動應用程序。

除了重新啟動活動之外,上述未決意圖為何不起作用的任何線索或任何其他建議? 我不想使用startActivity(intent)重新啟動

當你想重新啟動整個應用程序時,你可以使用非常簡單的庫: ProcessPhoenix

您可以簡單地插入庫並執行:

ProcessPhoenix.triggerRebirth(context);

或有特定意圖:

Intent nextIntent = //...
ProcessPhoenix.triggerRebirth(context, nextIntent);

這是以編程方式重啟 android 應用程序的最簡單方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM