tags:

views:

232

answers:

3

I have been working on a replacement for the stock Car Home app for a bit, and I am completely stumped on how to override the Home button so that my app will be brought back to the foreground whenever the phone is docked. This is the way that Car Home works, so there must be a way.

It seems that BroadcastReceivers won't work, because the intent that is broadcast whenever the Home button is pressed will still cause the default homescreen app to launch; I cannot prevent it. I can override the Home button from within my app, but that does me no good since this needs to work when the user is outside my app. Car Home also does not do anything weird like set itself as the default homescreen app while it's running (I checked the logcat to make sure).

I've been really beating my head against the wall on this. Does anyone have any ideas, or can you at least point me in the right direction?

+2  A: 

Unfortunately, there is no way in the public APIs to override the Home button without the user confirming it.

Your best bet would be to implement a CATEGORY_HOME Intent. This means when a user pressed Home they would be presented with the option to run the standard Home or yours and make yours the default if they wanted.

When your application was launched you could then check if the phone was docked. If the phone is not docked you could then open the standard Home screen and close your app before anything is displayed.

Dave Webb
The problem there is that my app is not meant to be a home replacement app. Plus, the idea of having the app launch automatically only to kill itself off and launch a different launcher app (and how would it know which one?) is not ideal. Not trying to shoot down your answer; believe me, I've contemplated doing it this way, but there has to be a better way to do this. Like I said, Car Home accomplishes this, and there is at least one other app on the market that does this as well.
BigFwoosh
One app I've seen that does this is Toddler Lock which does override the Home intent. It's possible that the Car Home uses an unpublished API to do what it does.
Dave Webb
A: 

You need to the correct intent filter in your manifest for the app to launch automatically when you dock the phone. Refer to http://developer.android.com/reference/android/content/Intent.html#CATEGORY_CAR_DOCK for the information.

Al
Maybe I need to clarify a bit... I already have an intent filter on my main activity for CATEGORY_CAR_DOCK, and my app launches when you put it in the car dock just like it's supposed to. The problem is that when you hit the Home button while the app is running, it should be brought back to the front just like what happens when Car Home is running. That's the part I'm stuck on.
BigFwoosh
+1  A: 

Well, after many months I have finally found the answer to this question. The key is the "android.dock_home" metadata element, found here:

http://developer.android.com/reference/android/content/Intent.html#METADATA_DOCK_HOME

By using this in your AndroidManifest.xml, you can make your dock application become the home application temporarily. To do this, add this line to the AndroidManifest.xml inside the Activity tags for the dock app activity:

<meta-data android:name="android.dock_home" android:value="true" />

If the value is set to true, as long as your phone is docked the Home button will return you to the dock app. After undocking, the Home button will take you back to your normal home app.

BigFwoosh