android兼容2.2及以前版本的退出方法
16lz
2021-01-24
首先,在首个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之前版本的退出代码
}
我的程序使用了上边的代码,目前没发现问题,希望能给纠结在这个问题上的人有所帮助。。。。
转自:
http://hi.baidu.com/304580500/blog/item/fe8b628edf04f3f8f01f36d9.html
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之前版本的退出代码
}
我的程序使用了上边的代码,目前没发现问题,希望能给纠结在这个问题上的人有所帮助。。。。
转自:
http://hi.baidu.com/304580500/blog/item/fe8b628edf04f3f8f01f36d9.html
更多相关文章
- Android(安卓)权限大全
- Android(安卓)(DiskLruCache)硬盘缓存代码实现
- Android获取状态栏的高度
- Android(安卓)init.rc文件解析过程详解(二)
- Android(安卓)Studio:Unable to instantiate application com.an
- Android(安卓)Apk加固(安全专项)
- Android系统源代码情景分析:基础知识
- AndroidStudio快捷键设置2
- 【翻译】(7)CPU Arch ABIs