进入后台
@Override
public void onBackPressed() {
Intent intent = new Intent();
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.HOME");
startActivity(intent);
}
双击两次退出
private static boolean mBackKeyPressed = false;//记录是否有首次按键
/** * 点击两次返回键 kill 程序 */ @Override public void onBackPressed() { if (!mBackKeyPressed) { Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show(); mBackKeyPressed = true; new Timer().schedule(new TimerTask() { @Override public void run() { mBackKeyPressed = false; } }, 2000); } else { this.finish(); System.exit(0); } }