简体   繁体   中英

Disable the back,recent apps button , power button, home and home long press button

Before asking this question I tried the many solutions related to this topic on StackOverflow but some solutions are confusing and some solutions are not working may be an android version issue.

Therefore, I asked this question so please help me to Disable the back, recent apps button, power button, home and home long press button when the activity launched.

The answer should work in android 4.4.x to above version .

Thanks.

Have you tried onBackPressed() function?

 @Override
    public void onBackPressed()
    {
         // code here to show dialog
         super.onBackPressed();  // optional depending on your needs
    }

The onBackPressed() function is called when the activity has detected the user's press of the back key. developer.android.com I am using this to disable back press button on Activity.

For Other like power button press, you can try below code.

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
        // do what you want with the power button
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

OR

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
        Intent i = new Intent(this, ActivitySetupMenu.class);
        startActivity(i);
        return true;
    }

    return super.dispatchKeyEvent(event);
}

You need to find out Android KeyEvent. KeyEvent.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM