强制退出整个应用的方法,参考http://263229365.iteye.com/blog/1283914
int currentVersion = android.os.Build.VERSION.SDK_INT;
if (currentVersion > android.os.Build.VERSION_CODES.ECLAIR_MR1) {
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
startActivity(startMain);
System.exit(0);
} else {// android2.1
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
am.restartPackage(getPackageName());
}
测试只有这个方法比较有效.能退出整个进程.关闭所有Activity.但是缺点是.直接回到idle.而不是mainmenu.
其余的如下
finish.
system.exit(0),
android.os.Process.killProcess(android.os.Process.myPid());
均只能结束当前的Activity.
比如task中有 A. B 2个Activity.在B中调用上述3种方式.都只能回到A.而不是退出app.
需要注意的是:finish是结束B,回到A. 后2者却是先结束了改app的进程.但是又回到了A.同时又启动了改app进程.在ddms中可以看到进程先消失再出现