Android中重启应用的几种方法

时间:2025-01-27 07:00:17

方法一:

Intent intent = new Intent(, );
startActivity(intent);
(0);
finish();

说明: 其中,LoginActivity为重启后的界面,点击重启功能之后,应用是不会走onDestroy()方法,直接走onCreate()–>onResume()。

方法二:

Intent intent = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
//与正常页面跳转一样可传递序列化数据,在Launch页面内获得
("REBOOT","reboot");
PendingIntent restartIntent = (getApplicationContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager mgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
(, () + 100, restartIntent);
(());

说明:点击重启功能之后,应用是不会走onDestroy()方法,直接走onCreate()–>onResume()。

方法三:

Intent intent = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//与正常页面跳转一样可传递序列化数据, 在Launch页面内获得
("REBOOT","reboot");
startActivity(intent);

说明:点击重启功能之后,应用是会走启动初始界面onCreate()–>onResume(),之后走最后activity的onDestory()方法。