Created
November 29, 2017 07:59
-
-
Save jie-meng/bfdd8f48818aa8b657522bb91436e366 to your computer and use it in GitHub Desktop.
Android Restart App
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void restart() { | |
killBackgroundProcesses(); | |
Runtime.getRuntime().addShutdownHook(new Thread() { | |
@Override | |
public void run() { | |
super.run(); | |
PackageManager packageManager = context.getPackageManager(); | |
Intent intent = packageManager.getLaunchIntentForPackage(context.getPackageName()); | |
ComponentName componentName = intent.getComponent(); | |
Intent mainIntent = Intent.makeRestartActivityTask(componentName); | |
context.getApplicationContext().startActivity(mainIntent); | |
} | |
}); | |
Runtime.getRuntime().exit(0); | |
} | |
static void killBackgroundProcesses() { | |
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); | |
List<ActivityManager.RunningAppProcessInfo> runningProcesses = manager | |
.getRunningAppProcesses(); | |
if (runningProcesses != null) { | |
for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) { | |
if (!BuildConfig.APPLICATION_ID.equalsIgnoreCase(processInfo.processName)) { | |
android.os.Process.killProcess(processInfo.pid); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment