2

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 . . .

Community
  • 1
  • 1
aftab
  • 1,141
  • 8
  • 21
  • 40

3 Answers3

3

You cannot "detect home key Button press event", sorry.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Hi Joel Martinez I want to save newly reset Options of user.I have a option screen,when user changes its old options setting and go on Mobile Main Home screen I want to save newly changed values at that time .... – aftab Jul 14 '11 at 13:35
  • 2
    @aftab: Then "save newly changed values" in `onPause()` of your activity. That will cover BACK, HOME, the user getting a phone call, the user tapping on a `Notification`, the user long-pressing HOME and going back to some other application, etc. – CommonsWare Jul 14 '11 at 13:37
  • thanks,But I test it with peer view,I override all onResume,OnPostResume,OnStart and Restart of my activity,I notice when control comes in my activity for the 1st time it goes on onResume method and when I press Home key it go onStop method not on onResume method,means when I press Home button it does not go in onResume method . . . – aftab Jul 14 '11 at 14:02
  • why do you want to run onResume method when you press Home key? if you want to do something when you press Home key, overide onPause() method.. although the app still minimized after that. – Tek Yin Jul 14 '11 at 17:35
  • @CommonsWare - technically your correct but you can get a pretty good idea that when it happens, not 100% full proof (well it's always worked for me but ... ) which may or may not meet the needs of the poster above, see my answer below. – Idistic Jul 14 '11 at 19:11
  • I update my ADT and eclipse Detail as below ,Mobile Product : Samsung Galaxy Model Number :GT-19000 Andoid Os version in Mobile : 2.2.1 Android Development Toolkit Version: 12.0.0.v201106281929-138431 Dalvik Debug Monitor Service Version: 12.0.0.v201106281929-138431 Traceview Version: 12.0.0.v201106281929-138431 Eclipse Java EE IDE for Web Developers. Version: Helios Service Release 2 Build id: 20110218-0911 – aftab Jul 16 '11 at 09:19
  • I am using tabView.Option Activity attached with my 4th Tab that also have ScrollView.I run my code in debug mode and place the breakpoint in onPause () method as I am overriding onPause () method of my fourth tab’s activity .When I press Home key control goes in onPause() method.Thanks CommonsWare – aftab Jul 16 '11 at 09:23
3

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!");  
}
Idistic
  • 6,281
  • 2
  • 28
  • 38
  • This strikes me as being unreliable. Anything that involves subtle timing is going to be prone to failure some percentage of the time. Moreover, I would consider any application that needs to handle the HOME button separately from other events to be broken -- there should be no difference between HOME, an incoming phone call, an alarm clock alarm, etc. – CommonsWare Jul 14 '11 at 19:14
  • @CommonsWare As to your last point perhaps, I kind of agree but then again my needs have to do with analytic's, not behavior, still I am not going to say outright that there is no other reason to use it. – Idistic Jul 14 '11 at 19:27
  • @CommonsWare - As to timing that was my first thought also, but after testing it pretty extensively in an attempt to break it I could not, that does not mean it is 100% reliable of course, could be any number of untested scenarios that would result in a fail. After some thought I don't think the timing is that subtle since the home key generates that first event, then the second, again it could fail and I certainly would not depend on it for anything critical. Typically in my testing the delta between the two events is 1 or 2 ms as reported by the system timer. – Idistic Jul 14 '11 at 19:32
  • "my needs have to do with analytic's, not behavior" -- ah, OK. For that, even if you are +/- on events occasionally, you're probably OK. – CommonsWare Jul 14 '11 at 20:28
  • @Idistic thanks for new idea.your solution looking quite ok,But its onUserInteraction() method working for Back-key press or when I scroll-over the UI,its not working when press the home key I moves among the different tabs,2nd in Debug mode control not go into onUserLeaveHint() method in any case. – aftab Jul 16 '11 at 09:33
  • @Idistic as per given description on this page it should work. http://docs.mono-android.net/Android.App.Activity.OnUserLeaveHint%20() – aftab Jul 16 '11 at 09:36
2

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.

Community
  • 1
  • 1
Marmoy
  • 8,009
  • 7
  • 46
  • 74
  • hi videre thanks,I just read a page from discussion as on below URL,Read the make dg comments on page,but I am not understanding what he/she saying in his/her comments , http://groups.google.com/group/android-developers/browse_thread/thread/1b10a441efec70c7 – aftab Jul 14 '11 at 13:40
  • He is saying that you can simulate the EFFECT of pressing the home button, setting the right action and category on your intent. It is not relevant to you because you want to REACT to a click on the home button. – Marmoy Jul 14 '11 at 14:09