Aug 29, 2013

How to Intercept HOME Key in Android?


The solution is:

The prevailing wisdom says it cannot be done

public static final int KEYCODE_HOME 
Key code constant: Home key. This key is handled by the framework and is never delivered to applications.
Maybe the below code would work the way we want it to. But I don't think you can trap the Home key absolutely.


Below method works for me. Hope this will work for you also....

Override below method in your activity. 

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


After overriding above method, now you can easily listen HOME Key press in your activity using onKeyDown() method.

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {     
     if(keyCode == KeyEvent.KEYCODE_HOME)
        {
           //The Code Want to Perform. 
        }
    });
 
 

No comments:

Post a Comment