1

Using the setOnKeyListener I can able to listen for all physical buttons except Home and End button, is there any possibility to catch the action of Home button.

Vignesh
  • 3,571
  • 6
  • 28
  • 44

4 Answers4

3

You may try this on Android 4.0+:
1. Register a BroadcastReceiver for Intent.ACTION_CLOSE_SYSTEM_DIALOGS.
2. Call Intent.getStringExtra("reason") to get the reason. Reasons are below:
"homekey" for home key pressed;
"assist" for home key long pressed;

2

You do not need to catch Home button. If user press Home and some other Activity comes to foreground, your app goes to background and onPause() is called in your current Activity. You may override that function to clean search string or anything you need.

UPDATE:

More clean solution is to use flag FLAG_ACTIVITY_NO_HISTORY when starting that critical activity. So, when your activity goes to background system will close it properly for you.

Zelimir
  • 11,008
  • 6
  • 50
  • 45
  • Hi Zelimir, I called finish() function inside onStop() function, so its working but it showing Sorry the application has stopped unexpectedly. – Vignesh May 06 '11 at 07:27
  • First line should be super.onPause(); and then you may call finish(). If that does not help please post the top line of the error report that contains useful info why it happened. – Zelimir May 06 '11 at 07:50
  • Hi Zelimir, finish() within onPause() finishes the activity during Orientation change, so I gave it inside onStop() and I'm getting Sorry the application closed unexpectedly. – Vignesh May 09 '11 at 04:19
  • Ok, but have you put firstly super.onStop() and then finish()? If yes, please post top of the error stack from the LogCat trace. It should be working without any issue. – Zelimir May 09 '11 at 07:10
  • Updated my answer with another, more clean approach. – Zelimir May 09 '11 at 07:15
  • Sorry Zelimir, in my application I've login activity which leads to activity containing user information,(Step 1) if user presses Home button from this activity it will come to Main screen, and(Step 2) if any one long press the Home button and presses my application icon it will directly show the user info, I tried all suggestions and I'm getting Application stopped unexpectedly at step 2 followed but 1. So I've right now I've given user info activity as Portrait and called the finish() functionality inside onPause(). – Vignesh May 11 '11 at 05:14
  • Why is it stopped unexpectedly? Do you have a LogCat trace? Can I help further? – Zelimir May 11 '11 at 06:24
  • Hi Zelimir, using FLAG_ACTIVITY_NO_HISTORY is working pretty fine, I don't know how do I miss that thing, sorry for wasting your time. – Vignesh May 11 '11 at 06:40
  • You have not wasted my time. I was just wondering why this approach does not work for you. Regards. – Zelimir May 11 '11 at 06:49
1

You want to use public boolean dispatchKeyEvent(KeyEvent event), as covered here: http://developer.android.com/reference/android/app/Activity.html#dispatchKeyEvent%28android.view.KeyEvent%29.

Use it like so:

    @Override
        public boolean dispatchKeyEvent(KeyEvent event)
        {
    // do whatever you want to do here, then return true if you handled the key code
if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (event.getKeyCode()) {
            case KeyEvent.KEYCODE_BACK:
                mBackDown = true;
                return true;
            case KeyEvent.KEYCODE_HOME:
                mHomeDown = true;
                return true;
            }
}
    return super.dispatchKeyEvent(event);  // let the default handling take care of it
    }

Let me know if that works for you.

EDIT: not sure why this doesn't work for you, but without looking through the rest of your code it would be hard to tell what exactly is going on. However, for your task, what I would recommend is that you use the finishOnTaskLaunch manifest attribute, as described at http://developer.android.com/guide/topics/manifest/activity-element.html#finish: properly used (set it to true) this will make sure that if your Activity is relaunched it will shutdown any existing instance.

Femi
  • 64,273
  • 8
  • 118
  • 148
  • Hi Femi, still it is not working for Home, End and Power buttons. – Vignesh May 06 '11 at 07:06
  • Odd: that does work for me, at least with the Home and Back buttons. I'm not sure about the END button: don't know that there is a physical button for END. Also, for HOME you might need to make your Activity a Home screen (otherwise I believe Android filters out that keypress). And I don't believe you can trap the POWER button: that would be a major security issue in my opinion: someone could write something that refused to let you turn your device off. So you might be out of luck on the power button. – Femi May 06 '11 at 13:15
  • Hi Femi, I don't need to listen for End button or Power button, I want to listen only the Home button, I'm just informing you that listener for Home, End and Power is not working. – Vignesh May 09 '11 at 04:16
  • Hi Femi, Zelimir's updated answer get worked for me, and thank your help. – Vignesh May 11 '11 at 06:42
  • From the help documentation: Key code constant: Home key. This key is handled by the framework and is never delivered to applications – Regis St-Gelais May 14 '12 at 20:13
0

This is only possible if you modify the main android source code. Although this is not recommended for app purposes. But more geared towards hidden menus.

public static final int KEYCODE_HOME

Since: API Level 1
Key code constant: Home key. This key is handled by the framework and is never delivered to applications.
Constant Value: 3 (0x00000003)
JoxTraex
  • 13,423
  • 6
  • 32
  • 45