-1

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.

Aada
  • 1,591
  • 7
  • 30
  • 57
  • The Home button event is not delivered to regular apps. http://code.google.com/p/android/issues/detail?id=1979 - You can't disable it. – zapl Nov 22 '12 at 10:39
  • In my app i am trying to disable the Home Button. which ofcourse it is doing it. but with this it is also disabling my back button which I dont want – Aada Nov 22 '12 at 10:41
  • Hi Zapl can you please explain what you mean by not delivered to the applications.?? Thanks – Aada Nov 22 '12 at 10:43
  • Means that `onKeyDown` is never called when the user presses the home button. So `event.getKeyCode() == KeyEvent.KEYCODE_HOME` will never be true. You disable the back button in there because in case of `event.getKeyCode() == KeyEvent.KEYCODE_BACK` you return true, which means that your code handled that event and it is not going through to the system that would otherwise go back. Remove that part and you will have the back button working again. – zapl Nov 22 '12 at 10:49
  • hi zapl.I just checked the logcat and analyzed that on pressing of home button it goes to the event.getKeyCode() == KeyEvent.KEYCODE_HOME section. And removing the back button section too is not solving my problem. Thanks btw – Aada Nov 22 '12 at 11:02

2 Answers2

1

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

DroidBender
  • 7,762
  • 4
  • 28
  • 37
  • OKye. Thanks for your response but when trying to work on the above code how it is disabling everything.? can't I somehow disable only the homebutton and not back key – Aada Nov 22 '12 at 10:50
  • I have updated my comment. As I already explained, the 'homebutton' functionality can not be changed by you. – DroidBender Nov 22 '12 at 10:54
  • Hi I tried doing the same by returning false instead of returning true. but still it wont work :) – Aada Nov 22 '12 at 11:16
  • Return to the super class, so it can be handled there. – DroidBender Nov 22 '12 at 11:50
0

Yes, it is possible. In you manifest, under your activity deceleration, replace

<category android:name="android.intent.category.LAUNCHER" />

with

<category android:name="android.intent.category.HOME" />

Note that with this change, your application will not be visible in the "All Apps" section. Also, user needs to set you application as the default home screen. Try out "Toddler Lock" application, it handles this scenario nicely.

PC.
  • 6,870
  • 5
  • 36
  • 71