实现账号冲突和账号更新时弹窗后的 App 重启

This commit is contained in:
844143714@qq,com 2026-04-30 02:28:41 +08:00
parent cc9783df1d
commit 29b31fbf69
2 changed files with 19 additions and 5 deletions

View File

@ -58,10 +58,10 @@ class MainActivity : AppCompatActivity() {
val ctx = instance ?: return@runOnUiThread val ctx = instance ?: return@runOnUiThread
androidx.appcompat.app.AlertDialog.Builder(ctx) androidx.appcompat.app.AlertDialog.Builder(ctx)
.setTitle("账号冲突") .setTitle("账号冲突")
.setMessage("您的 PTT 账号已在其他设备登录,当前设备已被迫下线。\n请点击确认退出并重新登录") .setMessage("您的 PTT 账号已在其他设备登录,当前设备已被迫下线。\n请点击确认重启应用")
.setCancelable(false) .setCancelable(false)
.setPositiveButton("确认") { _, _ -> .setPositiveButton("确认") { _, _ ->
com.stand.standapp.utils.AppKiller.killApp(ctx) com.stand.standapp.utils.AppKiller.killApp(ctx, restart = true)
} }
.show() .show()
} }
@ -76,7 +76,7 @@ class MainActivity : AppCompatActivity() {
.setMessage("您的 PTT 账号信息已被更新,需要重新登录以应用更改。\n请点击确认重启应用。") .setMessage("您的 PTT 账号信息已被更新,需要重新登录以应用更改。\n请点击确认重启应用。")
.setCancelable(false) .setCancelable(false)
.setPositiveButton("确认") { _, _ -> .setPositiveButton("确认") { _, _ ->
com.stand.standapp.utils.AppKiller.killApp(ctx) com.stand.standapp.utils.AppKiller.killApp(ctx, restart = true)
} }
.show() .show()
} }

View File

@ -7,11 +7,11 @@ object AppKiller {
private var isKilling = false private var isKilling = false
fun killApp(context: Context, isCrash: Boolean = false) { fun killApp(context: Context, isCrash: Boolean = false, restart: Boolean = false) {
if (isKilling) return if (isKilling) return
isKilling = true isKilling = true
Timber.tag("AppKiller").i("Starting app cleanup. isCrash: $isCrash") Timber.tag("AppKiller").i("Starting app cleanup. isCrash: $isCrash, restart: $restart")
try { try {
GpioManager.stopListening() GpioManager.stopListening()
@ -49,7 +49,21 @@ object AppKiller {
LogManager.shutdown() LogManager.shutdown()
} catch (e: Exception) {} } catch (e: Exception) {}
if (restart) {
try {
val intent = context.packageManager.getLaunchIntentForPackage(context.packageName)
if (intent != null) {
intent.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK or android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK)
val flags = android.app.PendingIntent.FLAG_ONE_SHOT or android.app.PendingIntent.FLAG_IMMUTABLE
val pendingIntent = android.app.PendingIntent.getActivity(context, 223344, intent, flags)
val mgr = context.getSystemService(Context.ALARM_SERVICE) as android.app.AlarmManager
mgr.set(android.app.AlarmManager.RTC, System.currentTimeMillis() + 500, pendingIntent)
}
} catch (e: Exception) {}
}
android.os.Process.killProcess(android.os.Process.myPid()) android.os.Process.killProcess(android.os.Process.myPid())
System.exit(0)
} }
fun setupUncaughtExceptionHandler(context: Context) { fun setupUncaughtExceptionHandler(context: Context) {