Overriding Home button for a Car Home replacement app
Asked Answered
F

4

5

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).

What can I try next?

Fulmer answered 19/5, 2010 at 3:21 Comment(0)
F
9

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.

Fulmer answered 25/8, 2010 at 14:2 Comment(21)
how to use this meta-data to override the home button. i use it but it does not work for me. please give full code which you write in the activity.Childlike
Basically, in your AndroidManifest.xml, you'll want to edit the xml for the Activity that you want to use as the dock home like this: <activity> <meta-data android:name="android.dock_home" android:value="true" /> </activity> I'm not near my machine right now so I can't give you the full code, but as long as you put that line inside the Activity tags then you should be good to go.Fulmer
<activity android:name=".Home" android:label="@string/app_name"> <meta-data android:name="android.dock_home" android:value="true" /> </activity> I am using this code in the manifest but when i press home button of device then application goes in background please help if you have done this functionalityChildlike
Is your phone in a car dock, and does the dock enable car mode automatically?Fulmer
No my phone is not in the Car Dock Mode. I want to enable that programitically. Is this possible If yes then how can i implement this?Childlike
If the device is pre-Froyo: Intent dockIntent = new Intent(); dockIntent.setAction(Intent.ACTION_DOCK_EVENT); dockIntent.putExtra(Intent.EXTRA_DOCK_STATE,Intent.EXTRA_DOCK_STATE_CAR); sendStickyBroadcast(dockIntent); If it is Froyo or higher: UiModeManager manager = (UiModeManager) getSystemService(Context.UI_MODE_SERVICE); manager.enableCarMode(0);Fulmer
Thanks it works. It will open the app of car home? Can I override to my screen if possible then please tell me. You are really genious to override this home button. we are very near to our questionChildlike
You need to add this Intent filter to your main Activity in the AndroidManifest file: <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> <category android:name="android.intent.category.CAR_DOCK"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> When the phone is put into car mode, the user needs to make your app the default car mode app, and the Home button will be overridden.Fulmer
I have put this code into the main Activity then it does not works again same problem arise that it will open the CarHome application screen not my application screen. I have edited the question with the given manifest please check it outChildlike
Using the code you wrote above, here's how it should look: <activity android:name=".Home" android:label="@string/app_name"> <meta-data android:name="android.dock_home" android:value="true" /> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> <category android:name="android.intent.category.CAR_DOCK"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </activity>Fulmer
I don't know where is the problem. I think i miss something in code i have used only UiModeManager manager = (UiModeManager) getSystemService(TextQ_Home.UI_MODE_SERVICE); manager.enableCarMode(0); code to enable the car mode except this i have wrote nothing. Is there any other thing which i miss here?Childlike
It shows the carhome application when i pressed the home button? is this possible that rather then carhome application my application layout will be shown?Childlike
If you have the above code in your manifest, and you enable car mode, it should prompt you to choose between Car Home or your app. Does it not do that?Fulmer
I'm not understanding your question... also can you please answer my question? It would help diagnose your problem.Fulmer
firstly i am sorry to irritate you again and again. i want to ask when my application starts then it starts with ACTION_MAIN, and CATEGORY_DOCK very well. but when i press home key then it fire ACTION_MAIN and CATEGORY_HOME. i want to override this thing.why my application runs this home button action if my application starts in the CAR_DOCK category. please help. i am become fan of you after seeing your application custom car home. please help only you can help me to solve my problem no anyother else.Childlike
No problem man, I'm glad to help. If you have <meta-data android:name="android.dock_home" android:value="true" /> in your manifest as shown above, your app is set to be the default app when in car mode, and your app is running while your phone is in car mode, then the Home button should bring your app to the foreground. That's the only circumstance where the Home button will work the way you want it to. Are you absolutely sure that you are meeting those conditions? Also, what device are you testing this on?Fulmer
I am testing this on the device htc desire. according to me i have use all things as you say in above but when i press the home key then launcher will run how can i disable it?Childlike
Thanks a lot very much at last i have done this task. after doing lots of research and your help i have solve this problem. :) thanks again.Childlike
can you please send me your email id for further assistance.Childlike
This manifest item was the missing piece for me. Thanks for posting. By the way, to stop being the default home app, even when in the car dock, UiModeManager manager = (UiModeManager) getSystemService(Context.UI_MODE_SERVICE); manager.disableCarMode(0);Sweeten
Does not work in Android 4.2: code.google.com/p/android/issues/detail?id=39765Intrados
R
2

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.

Redfin answered 19/5, 2010 at 6:13 Comment(2)
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.Fulmer
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.Redfin
D
2

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.

Decanter answered 19/5, 2010 at 14:57 Comment(1)
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.Fulmer
O
0

I found a way to tackle HOME key. For your application set the manifest as

<action android:name="android.intent.action.MAIN" />                              
<category android:name="android.intent.category.HOME" />                                
<category android:name="android.intent.category.DEFAULT" />                            
<category android:name="android.intent.category.MONKEY"/> 

Now your application is an alternate Launcher application.

Use the adb, and disable the launcher application using package manager

pm disable com.android.launcher2.

Now the Home key press will aways stay in the same screen.

Ostracon answered 27/12, 2010 at 11:54 Comment(2)
That does force the Home button to always launch your app, but it disables your normal launcher app. That wasn't desirable for me since I only wanted the Home button to change its behavior when the phone is in a docked state.Fulmer
@Ostracon thanx looking for same need to disable launcher. can you provide some details how to disable launcherGobbledegook

© 2022 - 2024 — McMap. All rights reserved.