back双击退出

安卓手机的back返回键 方法是OnkeyDown

private long exitTime;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode==KeyEvent.KEYCODE_BACK){
            if (System.currentTimeMillis() - exitTime > 2000 ){
                Toast.makeText(this,"再次点击退出应用", Toast.LENGTH_SHORT).show();
           
                localBroadcastManager.sendBroadcast(intent);
                return true;
            }else {

                finish();
            }
        }
        return super.onKeyDown(keyCode, event);

    }

弹出对话框的退出

   public long exitime;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK){
            if (System.currentTimeMillis()-exitime>2000){
                final AlertDialog.Builder builder = new AlertDialog.Builder(ZhuActivity.this);
                builder.setTitle("是否要返回");
                builder.setMessage("点击选择……");
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialog.cancel();
                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        finish();
                    }
                });

                dialog= builder.create();
                dialog.show();
            }else{

            }
        }
            return super.onKeyDown(keyCode, event);
    }
上一篇:Android模拟音量按键- 控制系统音频 包括外接音响


下一篇:Android-Keyevent对应键值记录