android2.2退出程序的例子

经过很长时间的研究,终于把2.2的退出实现了
首先,在首个activity中写:

Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//别忘了这行,否则退出不起作用
intent.setClass(getApplicationContext(), HomeActivity.class);
startActivity(intent);

其次,在HomeActivity需要退出的地方写:

Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
System.exit(0);//退出程序

顺便把2.2之前版本的退出也写下来:

ActivityManager activityMgr= (ActivityManager) getSystemService(ACTIVITY_SERVICE );
activityMgr.restartPackage(getPackageName());

如果你的程序既想兼容2.2又想兼容之前版本的话,需要你进行版本判断

int sdk_Version=android.os.Build.VERSION.SDK_INT;
if(sdk_Version==8){
//2.2

//写2.2的退出代码

}else if(sdk_Version<8){

//2.2之前版本

//写2.2之前版本的退出代码

}

我的程序使用了上边的代码,目前没发现问题,希望能给纠结在这个问题上的人有所帮助。。。。

posted @ 2011-12-13 08:58  囧里个囧  阅读(237)  评论(0)    收藏  举报