Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Hi I am looking for disabling the Only HomeKey In Android. WHat i am trying to do is

@Override
    public void onAttachedToWindow() {
        this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
        super.onAttachedToWindow();
    }
@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {



        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {

            return true;
        }

        if (event.getKeyCode() == KeyEvent.KEYCODE_MENU) {
                        //action
        }
        if (event.getKeyCode() == KeyEvent.KEYCODE_HOME) {
                        //action
        }
        if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_CENTER) {
            //action
        }

        return false;
    }

But by this my back button is too getting disabled. Is there any way to just disable the HomeKey in android. Please reply.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
186 views
Welcome To Ask or Share your Answers For Others

1 Answer

The documentation states:

Home key. This key is handled by the framework and is never delivered to applications.

More specifically, the key is consumed by the PhoneWindowManager in the method interceptKeyBeforeDispatching(..). In this method the key is handled and consumed, meaning that Android does not allow overriding this functionality.

UPDATE:

The reason why your back-behavior does not work anymore is because you yourself have handled it. The key enters the onKeyDown-method and you can consume the key (yes or no) by returning true or false. By implementing this:

 if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {

       return true;
 }

You explicitly state that you have handled the Back key in your Activity which also means that the default behavior of "going-back" is overridden. To restore to its original behavior use return super.onKeyDown(keyCode, event);


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...