如何重置默认的启动/主屏幕更换?屏幕

2023-09-05 00:14:48 作者:情场书生

当用户点击首页,他被赋予发射的选择,他还可以选择是否将其设为默认值。问题是,以后很难再改变。

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.

要解决这个问题,我添加了重设preferred发射触发这样的:

To fix this I added a "Reset preferred launcher" that triggers this:

getPackageManager().clearPackagePreferredActivities(getPackageName());

不过这条线只复位preferred发射,如果他选择了我的发射。我需要一个片段,清除preferred发射不管它是什么,所以下一次用户点击的家,他再次给出的选项。

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.

推荐答案

这不是直接可能,而Android开发者都表示,他们不希望任何应用程序更改用户preferences。然而,有一种变通方法的基础上如何保持Android的这些preferences。

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.

请您的清单是这样的:

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

无论如何,一旦你的清单看起来是这样,你可以使用清除默认启动code是这样的:

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

通过启用一个别名,禁止其他,可以使机器人感知用户的选项已经改变,因为如果你安装了一个发射器和卸载另一个。因此,用户将被要求在下一次preSS home键再选择。此方法适用无论是谁发射器是当前的默认。

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.