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

来源:互联网 发布:js format date 编辑:程序博客网 时间:2024/05/16 14:20
 经过多次试验,终于自己解决了
首先在要退出的地方写
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"
这个所有版本的系统都适用,经过测试完全退出同时杀掉进程
原创粉丝点击