Skip to content

代码重启app的方法

使用PackageManager实现重启app的功能。

具体步骤是,先startActivity启动自己app,然后退出当前进程。

public static void triggerRebirth(Context context) {
    PackageManager packageManager = context.getPackageManager();
    Intent intent = packageManager.getLaunchIntentForPackage(context.getPackageName());
    ComponentName componentName = intent.getComponent();
    Intent mainIntent = Intent.makeRestartActivityTask(componentName);
    context.startActivity(mainIntent);
    Runtime.getRuntime().exit(0);
}

参考: https://stackoverflow.com/questions/6609414/how-do-i-programmatically-restart-an-android-app