0

I want to disable home button action using the accessibility service. I already read the docs from https://developer.android.com/training/accessibility/service

but didn't understand how to do it? Can anyone help me out in this

Android Guy
  • 573
  • 1
  • 8
  • 18

3 Answers3

2

Create your own Accessibility Service using any tutorial and put this code inside it....

public class button_lock extends AccessibilityService {
@Override
public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {

}

@Override
public void onInterrupt() {

}
@Override
protected boolean onKeyEvent(KeyEvent event) {
    if(event.getKeyCode()==KeyEvent.KEYCODE_HOME)
        return true;
    else
       return super.onKeyEvent(event);
}}

Then enjoy....

Biswajit
  • 25
  • 7
  • 2
    This only worked for the me on a device with physical touch buttons. When tried with another device with only navigation bar, didn't work. – Cesar Mauri Jul 22 '19 at 18:17
  • Can you share also the xml config file for the upper code? Without it, it does not work at all, even any key is not received – Duna Aug 07 '19 at 11:30
  • 2
    @CesarMauri sorry to say but this code works for only physical buttons – Biswajit Oct 15 '19 at 09:34
-1

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;
}
urvi joshi
  • 158
  • 13
  • Cannot resolve symbol 'TYPE_KEYGUARD', should I use 'TYPE_KEYGUARD_DIALOG' in place of this? – Android Guy Jul 09 '18 at 09:39
  • For Api above 14 use @AndroidGuy – urvi joshi Jul 09 '18 at 09:41
  • You can also refer this https://abhik1987.wordpress.com/tag/android-disable-home-button/ – urvi joshi Jul 09 '18 at 09:44
  • ok let me give a try and one more thing I don't want o jump to main screen on clicking the home button. What should I do in if(keyCode == KeyEvent.KEYCODE_HOME) { Log.i("Home Button","Clicked"); } – Android Guy Jul 09 '18 at 09:47
  • Don't add anything inside that condition. @AndriodGuy – urvi joshi Jul 09 '18 at 09:49
  • Getting this error E/WindowManager: Window : Window{a70c6e9 u0 com.lockscreen.maroof/com.lockscreen.maroof.LockActivity}changes the window type!! Original type : 1 Changed type : 2009 Window Session Crash java.lang.IllegalArgumentException: Window type can not be changed after the window is added. – Android Guy Jul 09 '18 at 09:54
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/174643/discussion-between-urvi-joshi-and-android-guy). – urvi joshi Jul 09 '18 at 09:56
  • The subject of the question is: AccesibilityService, not an Activity – Duna Nov 30 '18 at 19:14
  • What should we declare on xml config for accservice? – Duna Jul 18 '19 at 20:30
-1

You can Disable Home Key by adding this code in your activity:

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

However, this doesn't work while dialog is showing. To get rid of this problem simply add my code to your Dialog object:

Disable home Key while Dialog showing

Adrian W
  • 4,563
  • 11
  • 38
  • 52