|
|
@@ -5,6 +5,8 @@ import android.Manifest.permission.ACCESS_FINE_LOCATION
|
|
|
import android.Manifest.permission.BLUETOOTH_ADVERTISE
|
|
|
import android.Manifest.permission.BLUETOOTH_CONNECT
|
|
|
import android.Manifest.permission.BLUETOOTH_SCAN
|
|
|
+import android.app.AlarmManager
|
|
|
+import android.app.PendingIntent
|
|
|
import android.content.Context
|
|
|
import android.content.Intent
|
|
|
import android.os.Build
|
|
|
@@ -86,10 +88,43 @@ object CommonUtils {
|
|
|
}
|
|
|
|
|
|
fun restartApp(ctx: Context) {
|
|
|
- ctx.packageManager?.getLaunchIntentForPackage(ctx.packageName)?.let {
|
|
|
- it.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
|
|
|
- ctx.startActivity(it)
|
|
|
+ // 1. 拿到你的启动 Intent,并加上清除栈的 Flag
|
|
|
+ val launchIntent = ctx.packageManager
|
|
|
+ .getLaunchIntentForPackage(ctx.packageName)
|
|
|
+ ?.apply {
|
|
|
+ addFlags(
|
|
|
+ Intent.FLAG_ACTIVITY_NEW_TASK or
|
|
|
+ Intent.FLAG_ACTIVITY_CLEAR_TASK
|
|
|
+ )
|
|
|
+ } ?: return
|
|
|
+
|
|
|
+ // 2. 包装成一个一次性 PendingIntent
|
|
|
+ val pendingIntent = PendingIntent.getActivity(
|
|
|
+ ctx,
|
|
|
+ 0,
|
|
|
+ launchIntent,
|
|
|
+ PendingIntent.FLAG_CANCEL_CURRENT or PendingIntent.FLAG_IMMUTABLE
|
|
|
+ )
|
|
|
+
|
|
|
+ // 3. 通过 AlarmManager 延迟一点点时间触发重启
|
|
|
+ val alarmManager = ctx.getSystemService(Context.ALARM_SERVICE) as AlarmManager
|
|
|
+ val triggerAt = System.currentTimeMillis() + 100L // 延迟 100ms
|
|
|
+
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
|
+ alarmManager.setExactAndAllowWhileIdle(
|
|
|
+ AlarmManager.RTC_WAKEUP,
|
|
|
+ triggerAt,
|
|
|
+ pendingIntent
|
|
|
+ )
|
|
|
+ } else {
|
|
|
+ alarmManager.setExact(
|
|
|
+ AlarmManager.RTC_WAKEUP,
|
|
|
+ triggerAt,
|
|
|
+ pendingIntent
|
|
|
+ )
|
|
|
}
|
|
|
- exitProcess(0)
|
|
|
+
|
|
|
+ // 4. 杀掉当前进程,让 AlarmManager 去重启
|
|
|
+ android.os.Process.killProcess(android.os.Process.myPid())
|
|
|
}
|
|
|
}
|