How to reset default launcher/home screen replacement?

别等时光非礼了梦想. 提交于 2019-11-28 08:51:12

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.

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.

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?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!