ZiviMagic
ZiviMagic

Reputation: 19

Catching when the user pressed the Home Button

I would like to know when the user pressed the home button while he was running my app. BUT: The problem is that I don't want to edite the existing code. meaning I don't want to add logic to the existing activities onPause() method.

The only solution I found was to add a service to the application which listens to the Log detecting if there was an intent to run the

HOME: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME]

Is there any other way to do it or is it really impossible? The optimal solution would have been to catch an intent in the Manifest.xml, like:

action android:name="android.intent.category.HOME"

and implement a new class to catch it. (But it doesn't seem to catch it).

Upvotes: 1

Views: 560

Answers (2)

CommonsWare
CommonsWare

Reputation: 1006674

I would like to know when the user pressed the home button while he was running my app.

What specifically are you trying to achieve?

BUT: The problem is that I don't want to edite the existing code.

By definition, that is impossible.

The only solution I found was to add a service to the application which listens to the Log detecting if there was an intent to run the

That is modifying the code, violating your own requirement. Moreover, it requires a permission that really you should not be asking for.

Is there any other way to do it or is it really impossible?

That depends on what specifically you are trying to achieve.

The optimal solution would have been to catch an intent in the Manifest.xml...and implement a new class to catch it. (But it doesn't seem to catch it).

HOME is a category. It is not an action. Home screens are activities that respond to the MAIN action in the HOME category. However, it is modifying the code, violating your own requirement.

Upvotes: 1

Oguz Özkeroglu
Oguz Özkeroglu

Reputation: 3057

You can use key press event handler

public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_HOME) {
            // Home key pressed
        } else {
            return super.onKeyDown(keyCode, event);
        }
        return false;
    }

Upvotes: 0

Related Questions