0

I know this question has been asked many times but none of the solution provided seems to work for me. I have tried here,here and here

My requirement :- I want to over-ride the HOME BUTTON and the RECENT APPS button on android. I know this not a best but the requirement is as such that I have to do it.

My Code :-

@Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {


        if(keyCode == KeyEvent.KEYCODE_HOME)
        {
            Log.d("Key","Home button clicked");
            return false;
        }

        if(keyCode == KeyEvent.KEYCODE_MOVE_HOME)
        {
            Log.d("Key","Home button clicked Msg 2");
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

But the onKeyDown() never gets called. And I get IllegalArgumentException: Window type can not be changed after the window is added error.

I know one way to disable the HOME button is to make my app behave like an launcher app. But is there any better way to do it ?

Community
  • 1
  • 1

1 Answers1

1

You do not do it this way. You define one intent-filter for your Activity:

 <intent-filter>
      <action android:name="android.intent.action.MAIN" />

      <category android:name="android.intent.category.HOME" />
      <category android:name="android.intent.category.DEFAULT" />
 </intent-filter>
ligi
  • 39,001
  • 44
  • 144
  • 244
  • is this the only way ? – Xin Tao Sep 16 '15 at 17:51
  • @Iigi :- is this the only way ? I mean is there no other way ? I kind of already know this. The problem is that, in this approach, the activity lifecycle methods onPause() and onResume() gets called which I don't want. There is one method onUserLeaveHint() which get called when HOME button is called. Can't we override it to achieve the same result ?? – Xin Tao Sep 16 '15 at 18:03
  • @XinTao to do what you ask in the question AFAIK yes - but depends a bit on what you want to do - I assume a KIOSK style app? – ligi Sep 16 '15 at 18:29