Home key press Event Listener [duplicate]
Possible Duplicate:
How can I detect user pressing HOME key in my activity?
I am using below lines of code to find if the use press开发者_运维问答 the backkey from android phone,its working fine .
But I want to detect home key Button press event,Anyone can guide how it is possible ?
@Override
public void onBackPressed()
{
Toast.makeText(getApplicationContext(),"BackKeyPressed", Toast.LENGTH_LONG).show();
super.onBackPressed();
}
Thanks . . .
You cannot "detect home key Button press event", sorry.
While technically the people who responded are correct here is a simplistic way to detect the home key press by monitoring two events in your activity, it has worked for my simple needs and maybe it will work for yours as well.
I use a 100ms fence around the two events which I find always works for me. NOTE: I have only tested on a handful of phones, like all things in Android your mileage will vary dependent on OS / Hardware (heck even the stuff that's documented and supposed to work sometimes doesn't)
long userInteractionTime = 0;
@Override
public void onUserInteraction() {
userInteractionTime = System.currentTimeMillis();
super.onUserInteraction();
Log.i("appname","Interaction");
}
@Override
public void onUserLeaveHint() {
long uiDelta = (System.currentTimeMillis() - userInteractionTime);
super.onUserLeaveHint();
Log.i("bThere","Last User Interaction = "+uiLag);
if (uiDelta < 100)
Log.i("appname","Home Key Pressed");
else
Log.i("appname","We are leaving, but will probably be back shortly!");
}
No, it is not possible. From the documentation of the Home keycode: http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_HOME
public static final int KEYCODE_HOME
Key code constant: Home key. This key is handled by the framework and is never delivered to applications.
精彩评论