Android完全退出
經過多次試驗,終于自己解決了
首先在要退出的地方寫
int version = android.os.Build.VERSION.SDK_INT;
if (version <= 7) {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
manager.restartPackage(getPackageName());
}
else {
Intent startMain = new Intent();
startMain.setClass(this, MainActivity.class);
startMain.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(startMain);
}
然后在MainActivity寫
@Override
protected void onNewIntent(Intent intent) {
// TODO Auto-generated method stub
super.onNewIntent(intent);
if ((Intent.FLAG_ACTIVITY_CLEAR_TOP & intent.getFlags()) != 0) {
android.os.Process.killProcess(android.os.Process.myPid());
}
}
最后在Manifest設置MainActivity的Flags為:android:launchMode="singleTop"
這個所有版本的系統都適用,經過測試完全退出同時殺掉進程