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

作者: RustFisher
联系: rf.cs@foxmail.com
博客: rustfisher.com | RustFisher cnblog
示例: AndroidTutorial Gitee, Tutorial Github
链接: https://www.an.rustfisher.com/android/pm/restart-App/
一家之言,仅当抛砖引玉。如有错漏,还请指出。