关于Android退出程序的问题(在主页面或任意页面退出App)
2016-11-23 15:29
507 查看
在写Android退出App时,要注意是在App的主页面退出,还是任意页面退出,不要盲目的进行编码。 如点击App的“退出”按钮,页面跳转到登录页面,在登录页面退出App。在最初直接使用以下: Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); MyApplicatation.pool.shutdown();// 关闭线程池 android.os.Process.killProcess(android.os.Process.myPid()); 注意:在登录页面退出时,会从登录页面跳转到主页面,因此,不能直接以此方法。下面记录两种比较好用的退出方式。
1、以广播的方式
在BaseActivity(基类Activity)中 /** * 自定义广播接收者 ,以此来退出所有的Activity */ private BroadcastReceiver exitAppReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if ("exitApp".equals(intent.getAction())) { context.unregisterReceiver(this); finish(); } } }; private IntentFilter filter; if (filter == null) { filter = new IntentFilter(); filter.addAction("exitApp"); registerReceiver(exitAppReceiver, filter); } 在要退出的页面发送广播 Intent intent = new Intent(); intent.setAction("exitApp"); sendBroadcast(intent);
2、自定义AppLication来管理所有的Activity
在自定义的Application中, // 保存所有的Activity private List<Activity> activityList; /** * 添加activity到activityList集合中 * * @param activity * 每一個activity */ public void addActivity(Activity activity) { if (activityList == null) { activityList = new ArrayList<Activity>(); } activityList.add(activity); } public int getListSize() { if (activityList != null) { return activityList.size(); } return 0; } public void removeActivity(Activity activity) { if (activityList != null) { if (activityList.contains(activity)) { activityList.remove(activity); } } } /** 清空列表,取消引用 */ public void clearActivity() { activityList.clear(); } /** * app退出 */ public void exit() { for (Activity activity : activityList) { if (!activity.isFinishing() && activity != null) { activity.finish(); } } clearActivity(); System.exit(0); } /** * 结束指定类名的Activity * * @param cls */ public void finishActivity(Activity activity) { if (activity != null) { activityList.remove(activity); activity.finish(); activity = null; } } /** * 结束指定类名的Activity * * @param cls */ public void finishActivity(Class<?> cls) { for (int i = 0; i < activityList.size(); i++) { if (activityList.get(i).getClass().equals(cls)) { finishActivity(activityList.get(i)); } } } 在基类Activity(BaseActivity)中, protected MyApplicatation application; if (application == null) { application = (MyApplicatation) getApplicationContext(); } application.addActivity(this);
在退出页面:
if (null != application) {
application.exit();
}
双击退出程序
/**
* 对系统按键操作进行处理,双击退出
*/
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
// 按下的如果是BACK
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
back++;
if (back == 1) {
String appName = getResources()
.getString(R.string.app_name);
getToastdialog(“再按一次退出”);
// Toast.makeText(this, “再按一次退出” + appName, 1000).show();
new Handler().postDelayed(new Runnable() {
public void run() {
back = 0;
}
}, 2000);
}
if (back == 2) {
// 退出整个应用
not = false;
logOut(); } return false; } return super.dispatchKeyEvent(event); } return super.dispatchKeyEvent(event); } /** * 用户下线通知服务器 */ private void logOut() { new Thread() { public void run() { if (null != application) { application.exit(); } }; }.start(); }
相关文章推荐
- 关于android 多个Activity应用程序按返回键退出程序问题
- 关于程序退出无法关闭所有界面问题 通过广播使Android程序完全退出
- 关于android 多个Activity应用程序按返回键退出程序问题
- 关于Android程序退出后内存释放的问题
- 关于android程序完全退出的问题
- 关于Android程序退出后内存释放的问题
- 关于Android点击物理返回键退出程序的问题
- Android 关于极光推送退出APP时清空通知的问题
- Android关于Theme.AppCompat相关问题的深入分析
- Android关于Theme.AppCompat相关问题的深入分析
- 关于安卓APP底栏点击第一次选中当前Fragment第二次点击更新当前页面的问题(思路)
- 如何让程序在按了 Android 的 Back 键后不关闭当前 From 或者不 APP 退出
- 解决微信小程序中在其他页面无法使用app.js中获取的userInfo或openid的问题
- Android:后台给button绑定onClick事件、当返回项目到手机页面时提示是否退出APP
- 关于Android开发中使用System.exit(0)的问题(如写返回键响应时用到双击退出函数的时候)
- Android关于Theme.AppCompat相关问题的解决方案
- 关于 qt程序退出出现“已停止工作”程序奔溃问题 的解决方法
- 微信小程序——wx.navigateTo中url无法跳转问题(app.json中配置的tabBar与wx.navigateTo中url引用相同页面导致)(2017/12)
- 关于com.android.support:appcompat-v7:26.0.0以上无法下载的问题
- 关于com.android.support:appcompat-v7:27.+问题