简体   繁体   English

覆盖汽车之家更换应用程序的主页按钮

[英]Overriding Home button for a Car Home replacement app

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. 这是Car Home的工作方式,所以必须有办法。

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; 似乎BroadcastReceivers不起作用,因为每当按下Home按钮时广播的意图仍然会导致默认的主屏幕应用程序启动; 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). Car Home也没有做任何奇怪的事情,比如在运行时将自己设置为默认的主屏幕应用程序(我检查了logcat以确保)。

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? 有没有人有任何想法,或者你能否至少指出我正确的方向?

Well, after many months I have finally found the answer to this question. 好吧,经过好几个月,我终于找到了这个问题的答案。 The key is the "android.dock_home" metadata element, found here: 关键是“android.dock_home”元数据元素,可在此处找到:

http://developer.android.com/reference/android/content/Intent.html#METADATA_DOCK_HOME 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. 通过在AndroidManifest.xml中使用它,可以使dock应用程序暂时成为home应用程序。 To do this, add this line to the AndroidManifest.xml inside the Activity tags for the dock app activity: 为此,请将此行添加到Dock app活动的Activity标记内的AndroidManifest.xml中:

<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. 如果该值设置为true,只要您的手机停靠,主页按钮就会返回停靠应用。 After undocking, the Home button will take you back to your normal home app. 取消停靠后,主页按钮将带您回到普通的家庭应用程序。

Unfortunately, there is no way in the public APIs to override the Home button without the user confirming it. 不幸的是,公共API中没有办法在用户没有确认的情况下覆盖Home按钮。

Your best bet would be to implement a CATEGORY_HOME Intent. 您最好的选择是实现CATEGORY_HOME意图。 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. 这意味着当用户按下Home时,他们会看到运行标准Home或者你的选项,如果他们想要的话,你可以选择默认。

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. 如果手机未停靠,则可以在显示任何内容之前打开标准主屏幕并关闭应用程序。

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. 有关信息,请参阅http://developer.android.com/reference/android/content/Intent.html#CATEGORY_CAR_DOCK

I found a way to tackle HOME key. 我找到了解决HOME键的方法。 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 ur application is an alternate Launcher application. 现在你的应用程序是一个备用的Launcher应用程序。

Use the adb, and disable the launcher application using package manager 使用adb,并使用包管理器禁用启动器应用程序

pm disable com.android.launcher2. pm禁用com.android.launcher2。

Now the Home key press will laways stay in the same screen. 现在主页按键将保持在同一屏幕上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM