Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
189 views
in Technique[技术] by (71.8m points)

In Android Disable ONLY HomeKey Button

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

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

1 Reply

0 votes
by (71.8m points)

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);


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...