views:

320

answers:

3

I am working on an app that beeps every second. When I hit the home button I want it to close the program and stop beeping. Right now it closes the program but continues to beep.

What am I doing wrong?

        if ((keyCode == KeyEvent.KEYCODE_HOME)) {
        isdone = true;
        mp.release();
        counter.cancel();
        finish();
    }
A: 

When I hit the home button I want it to close the program and stop beeping.

You cannot intercept the HOME button from your application code -- sorry!

CommonsWare
A: 

You cannot intercept the HOME button directly, although a workaround would be to make your application start activity a HOME intent / default home activity; in your xml manifest, it would be like this:

<intent-filter>
    <action android:name="android.intent.action.MAIN"/>
    <category android:name="android.intent.category.LAUNCHER"/>
    <category android:name="android.intent.category.HOME"/>
    <category android:name="android.intent.category.DEFAULT"/>
</intent-filter>

So when the user presses the home button, you'd lead the user to the start activity of your app, from here you could redirect him to any other previous activity, i.e. by saving the user's state beforehand. In the activity that you mentioned above, where you have the beeping, you should place the stop command in onPause or onWindowFocusChanged.

This above approach only makes sense in certain cases though and depends on your application logic / business case and the environment you are going to deploy it.

We used this approach in an application (handling the home button) where our app will always be bundled and distributed with a device. So we're in full control of the device setup anyway. When you set your own activity as the default home activity the user would initially get an option screen with the question which activity should handle the home intent (the default android home or your app activity). Then the user can choose and also can check a checkbox to use this activity as the default activity in the future without being asked again.

For general apps for the public market, this might not be the best way though, as it would be little inconvenient and it's not a good screen flow / user experience either. For apps where you're in full control, this might be a suitable workaround.

If you really want to capture the HOME button, you would need to modify the android OS, it's in class PhoneWindowManager.java which comes in android.policy.jar. But this also only makes sense if you're in full control of the devices and i.e. bundle your app with a device or if it's an internal application where you deliver the devices to the users and are in control of which ROM you deploy.

There's also a useful discussion with further links about the HOME button on: http://www.anddev.org/home_menu_button-t10232.html

Mathias Lin
A: 

Isn't your activity's onPause() method called when you hit the home button?

You can kill the counter there. Don't kill the activity by yourself, the system will do that for you when it's appropriate.

macbirdie