//1.对话框退出App @Override public void onBackPressed() { new AlertDialog.Builder(this).setTitle("确认退出吗?") .setIcon(android.R.drawable.ic_dialog_info) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 点击“确认”后的操作 MainActivity.this.finish(); System.exit(0); } }) .setNegativeButton("返回", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 点击“返回”后的操作,这里不设置没有任何操作 } }).show(); //super.onBackPressed(); //2.再按一次退出 private long exitTime = 0; @Override public boolean onKeyDown ( int keyCode, KeyEvent event){ if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) { if ((System.currentTimeMillis() - exitTime) > 2000) { Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show(); exitTime = System.currentTimeMillis(); } else { moveTaskToBack(false); finish(); } return true; } return super.onKeyDown(keyCode, event); } }
退出App
最新推荐文章于 2023-08-12 18:09:02 发布