How to control Home Button in Android?

后端 未结 3 1753
不知归路
不知归路 2021-01-25 09:52

I am in my application and I have control back button but I also want to control home button. I have written the code but I am unable to control HOME button. Here is my code.

相关标签:
3条回答
  • 2021-01-25 10:53

    This solution works from 2.1,

    Okay, this was supposed to be a hard question. But here is a way to crack it.

    Override the below method in your Activity,

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

    And now handle the key event like this,

     @Override
     public boolean onKeyDown(int keyCode, KeyEvent event) {
    
       if(keyCode == KeyEvent.KEYCODE_HOME)
        {
         Log.i("Home Button","Clicked");
        }
       if(keyCode==KeyEvent.KEYCODE_BACK)
       {
    
            finish();
       }
    return false;
    

    };

    0 讨论(0)
  • 2021-01-25 10:55

    You cannot override the home key function, this functionality would allow an app to lock the user out of the phone.

    0 讨论(0)
  • 2021-01-25 10:57

    As per Android core concepts, no application can alter the behaviour of the Home button.

    This you may not achieve what you're trying to do.

    EDIT

    You can try to code one Home Screen Launcher so that when user presses the home button, they can go to your Home Launcher.

    0 讨论(0)
提交回复
热议问题