0

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.

lisovaccaro
  • 32,502
  • 98
  • 258
  • 410

2 Answers2

10

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.

j__m
  • 9,392
  • 1
  • 32
  • 56
  • I understand android developers not wanting an app changing the user preferences, but the lack of a central place to manage preferred apps is just ridiculous – lisovaccaro Mar 21 '13 at 03:34
  • I rather agree, but short of rooting your device there is nothing to be done about it. – j__m Mar 21 '13 at 03:36
  • I did more tests (added a white background to the theme) and the theme is working as I see the white background however the animations isn't (the activity is just fading in and out). I have no idea why this happens – lisovaccaro Mar 21 '13 at 04:44
  • I removed one of the aliases and in turn I'm toggling enabled/disabled of the activity itself and alias2. It seems to have fixed the issue. – lisovaccaro Mar 21 '13 at 04:49
  • Huh. I always kept the actual activity enabled so that if any other component needed to talk to it it wouldn't have to figure out which one was enabled. But if this works for ya... = ) – j__m Mar 21 '13 at 04:50
  • Just keep in mind the code I posted assumes that the component you're calling getComponentEnabledSetting() on was enabled by default. – j__m Mar 21 '13 at 04:51
  • This is a very awesome answer, however I came across a couple of bugs that I fixed through trial and error. Bug 1 is for some reason, Android no longer detects that you have a "Main Activity" to launch, and therefore will not launch it on installation. In fact, the app just disappears from your device, and you will be unable to find it or launch it for that matter. to circumvent this, I added: to my Activity. – Wakka02 Jul 09 '13 at 10:14
  • @Wakka02 this thread was specifically about a home screen, so the intent-filter in the example is one for a home screen, with category HOME. You should replace the entire intent-filter from the example with whatever filter you're currently using for your activity. – j__m Jul 13 '13 at 10:39
1

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?

Community
  • 1
  • 1
RoFF
  • 529
  • 5
  • 27