1. 程式人生 > >Android返回鍵後臺程式不退出

Android返回鍵後臺程式不退出

@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// 過濾按鍵動作
		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			moveTaskToBack(true);
		}
		return super.onKeyDown(keyCode, event);
	}
	@Override
	public void onBackPressed() {
		moveTaskToBack(true);
		super.onBackPressed();
	}

另外一種方式

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addCategory(Intent.CATEGORY_HOME);
        startActivity(intent);
        return true;
    }
    return super.onKeyDown(keyCode, event);
}