第一种方法:
public void onBackPressed() {
if (isState) { //isState初始值为true isState = false; Toast.makeText(this, "请双击back退出程序", Toast.LENGTH_SHORT).show(); new Timer().schedule(new TimerTask() { @Override public void run() { // TODO Auto-generated method stub isState = true; } }, 2000); } else { finish(); } }
第二种方法:
public class MainActivity extends Activity {
private Toast toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toast = Toast.makeText(getApplicationContext(), "确定退出?", 0);
}
public void onBackPressed() {
quitToast();
}
/*
public boolean onKeyDown(int keyCode, KeyEvent event) {
System.out.println(keyCode + "...." + event.getKeyCode());
if(keyCode == KeyEvent.KEYCODE_BACK){
quitToast();
}
return super.onKeyDown(keyCode, event);
}
*/
private void quitToast() {
if(null == toast.getView().getParent()){
toast.show();
}else{
System.exit(0);
}
}
}