java 如何重置默认启动器/主屏幕更换?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15537522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to reset default launcher/home screen replacement?
提问by lisovaccaro
When the user clicks Home he is given the choice of launcher and he can also choose whether to set it as default. The problem is that afterwards it's hard to change it again.
当用户单击主页时,他可以选择启动器,他还可以选择是否将其设置为默认值。问题是以后很难再改了。
To fix this I added a "Reset preferred launcher" that triggers this:
为了解决这个问题,我添加了一个“重置首选启动器”来触发:
getPackageManager().clearPackagePreferredActivities(getPackageName());
However this line only resets the preferred launcher if he has chosen my launcher. I need a snippet that clears the preferred launcher whatever it is, so next time the user clicks home he is given the options again.
但是,如果他选择了我的启动器,这条线只会重置首选启动器。我需要一个片段来清除首选启动器,因此下次用户单击主页时,他会再次获得这些选项。
回答by j__m
It's not directly possible, and Android developers have stated that they do not want any app changing the user's preferences. However, there is a workaround based on how Android maintains these preferences.
这不是直接可能的,Android 开发人员表示他们不希望任何应用程序改变用户的偏好。但是,有一种基于 Android 如何维护这些首选项的解决方法。
Make your manifest look like this:
让你的清单看起来像这样:
<activity
android:name="MyLauncherActivity"
android:exported="true" />
<activity-alias
android:name="LauncherAlias1"
android:targetActivity="MyLauncherActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity-alias>
<activity-alias
android:name="LauncherAlias2"
android:enabled="false"
android:targetActivity="MyLauncherActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity-alias>
For the sake of simplicity, I've left out additional attributes that aren't relevant to the task at hand.
为简单起见,我省略了与手头任务无关的其他属性。
Anyway once your manifest looks like this, you can clear the default launcher using code like this:
无论如何,一旦您的清单看起来像这样,您就可以使用如下代码清除默认启动器:
PackageManager pm = getPackageManager();
ComponentName cn1 = new ComponentName("com.mypackage", "com.mypackage.LauncherAlias1");
ComponentName cn2 = new ComponentName("com.mypackage", "com.mypackage.LauncherAlias2");
int dis = PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
if(pm.getComponentEnabledSetting(cn1) == dis) dis = 3 - dis;
pm.setComponentEnabledSetting(cn1, dis, PackageManager.DONT_KILL_APP);
pm.setComponentEnabledSetting(cn2, 3 - dis, PackageManager.DONT_KILL_APP);
By enabling one alias and disabling the other, you cause Android to perceive the user's options as having changed, as if you installed one launcher and uninstalled another. Thus, the user will be asked to choose again the next time they press the home button. This approach works no matter whose launcher is the current default.
通过启用一个别名并禁用另一个别名,您会导致 Android 将用户的选项视为已更改,就像您安装了一个启动器并卸载了另一个一样。因此,用户下次按下主页按钮时将被要求再次选择。无论谁的启动器是当前默认启动器,此方法都有效。
回答by RoFF
Alias activity will cause some problems like your launcher will provide two entry when picking up default launcher.
别名活动会导致一些问题,例如您的启动器在选择默认启动器时会提供两个条目。
following is my solution, it works for me. how to make the Launcher-Pick-Up popup window showing?
以下是我的解决方案,它对我有用。 如何使 Launcher-Pick-Up 弹出窗口显示?