views:

329

answers:

2

I need my activity to handle HOME button press with a receiver programmatically, but the event is not firing. I can, however, successfully register and capture this intent filter if I declare it in a manifest.xml activity section. Here's the code for the receiver that's not working:

BroadcastReceiver br;

br = new BroadcastReceiver() {
       @Override
       public void onReceive(Context context, Intent intent) {
               return;  // I put a breakpoint here to see if this gets called
       }
};


IntentFilter intf = new IntentFilter();
intf.addAction(Intent.ACTION_MAIN);
intf.addCategory(Intent.CATEGORY_HOME);
intf.addCategory(Intent.CATEGORY_DEFAULT);

registerReceiver(br, intf);

Does anyone know why this doesn't capture the home button click?

By the way, I tried working around this by creating another activity and specifying its intent filter in the manifest. I set the activity enabled="false" in the manifest but I couldn't figure out how to enable that activity at run time. Bottom line, I only want to register the home button intent filter for my application only when my application is running, and I can't figure out how to do that.

A: 

There should never be a case where you should need to override the home-button, it is there to make sure the user always maintain control over his phone. What exactly are you trying to do? If you want to detect when your activity are no longer visible, you should override onStop().

sandis
I want to add my action as a legitimate second source for the home button click, and I can easily do this if I put the intent filter in the manifest.xml action section. The only thing I want to do differently is I want this functionality ON DEMAND, because putting the filter into manifest.xml action registers my action for the home button click as long as the application is installed, and I don't want it to do that.
Alex
The Toddler Lock in the market is a great example of an app that legitimately over-rides the home button.
Jay Askren
A: 

If you are trying to implement a replacement home screen, you need your Intent filter to be in the manifest -- see the Home sample app in the SDK.

If you are trying to block the HOME key, please don't.

CommonsWare
Yes, it works in the manifest, but I want to turn it on/off programmatically at run time.Thank you for being polite :)
Alex
Yeah, unfortunately, I don't think that works. However, I missed part of your question: "I set the activity enabled="false" in the manifest but I couldn't figure out how to enable that activity at run time." -- you should be able to do this with `PackageManager`'s `setComponentEnabledSetting()` method.
CommonsWare
Thanks, I think it works!
Alex
I actually have a follow up question. How can I use setComponentEnabledSetting() for an activity-alias? The component name needs a class name, but the class for an alias is the same as a class for the targetActivity.
Alex
I posted the follow up question in a new thread: http://stackoverflow.com/questions/2231308/android-setcomponentenabledsetting-for-an-activity-alias
Alex