zoukankan      html  css  js  c++  java
  • android 完美退出应用程序。

    Android 程序在点击回退键时,如果只有一个activity,调用finish()方法就能退出界面,如果有多个界面,在调用该方法时,只会销毁当前的activity,显示栈顶的其它activity,换言之,就是无法退出整个应用程序。下面是一种快速的退出整个应用的方法代码:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    private void showTips() {
      
            AlertDialog alertDialog = new AlertDialog.Builder(this).setTitle("提醒")
                    .setMessage("是否退出程序")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
      
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent = new Intent(Intent.ACTION_MAIN);  
                            intent.addCategory(Intent.CATEGORY_HOME);  
                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
                            startActivity(intent);  
                        android.os.Process.killProcess(android.os.Process.myPid());
                        }
      
                    }).setNegativeButton("取消",
      
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            return;
                        }
                    }).create(); // 创建对话框
            alertDialog.show(); // 显示对话框
        }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            // TODO Auto-generated method stub
            if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
                showTips();
                return false;
            }
      
            return super.onKeyDown(keyCode, event);
        }
  • 相关阅读:
    mysql用 法like concat()
    redis系列之数据库与缓存数据一致性解决方案
    day33:进程锁&事件&进程队列&进程间共享数据
    day32:进程&进程join&守护进程deamon
    day31:socketserver&hashlib&hmac&TCP登录
    day30:TCP&UDP:socket
    day29:计算机网络概念
    小程序3:ATM小程序
    hdu 6867 Tree 2020 Multi-University Training Contest 9 dfs+思维
    Codeforces Round #660 (Div. 2) Captain Flint and Treasure 拓扑排序(按照出度、入读两边拓扑排序)
  • 原文地址:https://www.cnblogs.com/manmanlu/p/3905267.html
Copyright © 2011-2022 走看看