Android完全退出应用的方法

时间:2022-05-12 15:31:28

退出程序

public static void exitApp(Context context){
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.AppTask> appTaskList = activityManager.getAppTasks();
for (ActivityManager.AppTask appTask : appTaskList) {
appTask.finishAndRemoveTask();
}
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}

重启应用

public static void restartApp(){
//定时重启
Intent intent = new Intent(BaseApplication.getAppContext(), MainActivity.class);
PendingIntent restartIntent = PendingIntent.getActivity(
BaseApplication.getAppContext(),
-1,
intent,
PendingIntent.FLAG_ONE_SHOT);
AlarmManager mgr = (AlarmManager) BaseApplication.getAppContext().getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 10 * 1000, restartIntent);
}