下文转自:

http://liuweisir.iteye.com/blog/890309

-----------------------------------------------------------------------------------------------------

经过很长时间的研究,终于把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之前版本的退出代码

}

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

更多相关文章

  1. android 获取正在运行的应用程序列表
  2. Android预安装可卸载程序
  3. Android 功能代码总结
  4. 获取当前应用的版本号/应用名称
  5. android各版本的兼容问题
  6. Android按返回键,程序进入后台运行,不关闭程序,finishAcrivity

随机推荐

  1. Android引路蜂地图开发示例:基本知识
  2. Android(安卓)Application对象必须掌握的
  3. Android(二)数据存储和访问 之文件
  4. android编译系统分析
  5. Android小项目合集(经典教程)包含十五个And
  6. android style(样式)和theme(主题)设置
  7. android:shape的使用
  8. Android(安卓)JNI打印c\c++日志信息
  9. Android提权原理
  10. Android(安卓)SDK中tools详解