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.......
this works great... exactly the thing i was looking for... thanks...
ReplyDeleteThanks a lot ,,,,,,
ReplyDeleteWhen 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?
ReplyDeleteBecause of onAttachedToWindow() view is attached to a window. Returning true/false from onKeyDown does not make any effect.
DeleteOh 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.
Deletehey my service closed prematurely on start up here is my code
ReplyDeletepackage 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);
}
}
તમારો ખુબ ખુબ આભાર...
ReplyDeleteI am glad you pointed that out. Thanks :)
ReplyDeleteHow to capture home button pressed on ics ?
ReplyDeleteThankx a lot!!
ReplyDeleteThis Code is not work in to the micromax canvas HD(Androd 4.1.2)
ReplyDeleteThanks
ReplyDeletefor me, it is getting run time exception.
ReplyDeletejava.lang.IllegalArgumentException: Window type can not be changed after the window is added.
TYPE_KEYGUARD Value is removed now. any alternative of this value
ReplyDelete