public static void restart(Context context) {// 获取启动的intentIntent intent = PackageManager().PackageName());PendingIntent restartIntent = Activity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);// 设置杀死应用后2秒重启AlarmManager mgr = (AlarmManager) SystemService(context.ALARM_SERVICE);mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 2000, restartIntent);// 重启应用android.os.Process.killProcess(android.Pid());
}
或者先延迟几秒再杀死应用,先弹个Toast提示下
public static void restart(Context context) {Toast.makeText(context, "应用重启中..", Toast.LENGTH_SHORT).show();new Handler().postDelayed(new Runnable() {@Overridepublic void run() {// 获取启动的intentIntent intent = PackageManager().PackageName());PendingIntent restartIntent = Activity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);// 设置杀死应用后2秒重启AlarmManager mgr = (AlarmManager) SystemService(context.ALARM_SERVICE);mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 2000, restartIntent);// 重启应用android.os.Process.killProcess(android.Pid());}}, 2000);
}
本文发布于:2024-01-29 01:01:57,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170646132211585.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |