重写OnKeyDown()方法
退出提示框:
public booleanonKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
dialog();
return true;
}
return true;
}
protected void dialog() {
AlertDialog.Builder builder = new Builder(AccoutList.this);
builder.setMessage("确定要退出吗?");
builder.setTitle("提示");
builder.setPositiveButton("确认",
new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
android.os.Process.killProcess(android.os.Process.myPid());
}
});
builder.setNegativeButton("取消",
new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.create().show();
}
提示在按一次退出:
long temptime = 0;
public boolean onKeyDown(int keyCode,KeyEvent event){
if((keyCode==KeyEvent.KEYCODE_BACK)&&(event.getAction()==KeyEvent.ACTION_DOWN)) { // 2s内再次选择back键有效
if(System.currentTimeMillis() - temptime >2000)
{ System.out.println(Toast.LENGTH_LONG);
Toast.makeText(this, "请在按一次返回退出", Toast.LENGTH_LONG).show();
temptime = System.currentTimeMillis();
}
else {
finish();
System.exit(0); //凡是非零都表示异常退出!0表示正常退出!
}
return true;
}
return super.onKeyDown(keyCode, event);
}