您的位置:首页 > 移动开发 > Android开发

android2.3绝杀退出问题,此方法证实有效,可以完美退出

2011-09-08 16:21 399 查看
经过多次试验,终于自己解决了

首先在要退出的地方写

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"

这个所有版本的系统都适用,经过测试完全退出同时杀掉进程
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: