Pages

Thursday, January 5, 2012

Intercept HOME Key: Android


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. 
        }
    });
 
 
 
 
Cheers....... 

13 comments:

  1. this works great... exactly the thing i was looking for... thanks...

    ReplyDelete
  2. When I use the above method(setting Window type to TYPE_KEYGUARD and ovverriding onKeyDown), the app doesn't go to the background even when I return false from onKeyDown(which should invoke default behaviour for Home Key). Any suggestions?

    ReplyDelete
    Replies
    1. Because of onAttachedToWindow() view is attached to a window. Returning true/false from onKeyDown does not make any effect.

      Delete
    2. Oh OK.. So how do I get the Home key to work normally? I see that calling super.onKeyDown(keyCode, event) works for the Back key, but for Home key nothing happens.

      Delete
  3. hey my service closed prematurely on start up here is my code

    package com.example.hidekey;

    import android.os.Bundle;
    import android.app.Activity;
    import android.view.KeyEvent;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.WindowManager;
    import android.support.v4.app.NavUtils;

    public class hidek extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_hidek);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_hidek, menu);
    return true;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_CALL || keyCode == KeyEvent.KEYCODE_ENDCALL || keyCode == KeyEvent.KEYCODE_SETTINGS) {
    //preventing default implementation previous to android.os.Build.VERSION_CODES.ECLAIR
    return true;
    }
    return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onBackPressed() {
    }

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

    }

    ReplyDelete
  4. તમારો ખુબ ખુબ આભાર...

    ReplyDelete
  5. I don't believe this works anymore. IllegalArgumentException due to onAttachedToWindow() call to getWindow().setType(...). Works on everything before ICS 4.0.3.

    ReplyDelete
    Replies
    1. I am glad you pointed that out. Thanks :)

      Delete
  6. How to capture home button pressed on ics ?

    ReplyDelete
  7. This Code is not work in to the micromax canvas HD(Androd 4.1.2)

    ReplyDelete