How to disable the home key

I'd like to lock the screen. I want to disable the home key and only use the back key. How do I accomplish this?


Use this method to disable the Home key in android

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

I found a way to tackle the HOME key. For your application set the manifest as

<action android:name="android.intent.action.MAIN" />              
<category android:name="android.intent.category.HOME" />                 
<category android:name="android.intent.category.DEFAULT" />               
<category android:name="android.intent.category.MONKEY"/>

Now your application is an alternate Launcher application.

Use the adb, and disable the launcher application using package manager

pm disable com.android.launcher2

Now the Home key press will always stay in the same screen.


This solution works up to 3.x only.

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