2

i'm creating a launcher app and i would like to show the default launcher chooser manually.

I already found and tried this but this is not perfect, since i cannot select ALWAYS immediately. After choosing the launcher i need to press the home button to be able to select ALWAYS.

This is my code:

public static void resetPreferredLauncherAndOpenChooser(Context context) {
    PackageManager packageManager = context.getPackageManager();
    ComponentName componentName = new ComponentName(context, DefaultLauncherHelperActivity.class);
    packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

    Intent selector = new Intent(Intent.ACTION_MAIN);
    selector.addCategory(Intent.CATEGORY_HOME);
    selector.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(selector);

    packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, PackageManager.DONT_KILL_APP);
}

and here the manifest:

<activity
        android:name=".activities.MainActivity"
        android:launchMode="singleTask">
        <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>
<activity
        android:name=".activities.DefaultLauncherHelperActivity"
        android:enabled="false">
        <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>

This is what i get (left) vs. what i'm trying to achieve (right)

There has to be a way since Microsoft's launcher and the poco launcher are able to do this but i couldn't find a solution and unfortunately there is no open source launcher with the same functionality. Can anyone help me with this?

Özgür
  • 121
  • 1
  • 6

1 Answers1

0

There is no straight forward API available for this. However, calling below code before resetting preferred launcher will result in chooser dialog as per right side image.

Use it cautiously as calling this will actually resets user home app preference and effect can be seen outside of your home app as well.

try {
      IntentFilter intentFilter = new IntentFilter();
      intentFilter.addAction(Intent.ACTION_MAIN);
      intentFilter.addCategory(CATEGORY_HOME);
      intentFilter.addCategory(CATEGORY_DEFAULT);
      Intent intent1 = new Intent(Intent.ACTION_MAIN);
      intent1.addCategory(CATEGORY_HOME);
      intent1.setComponent(null);
      Class<?> clazz = Class.forName("android.app.AppGlobals");
      Class.forName("android.content.pm.IPackageManager")
           .getMethod("setLastChosenActivity", new Class[] { Intent.class, String.class, int.class, IntentFilter.class, int.class, ComponentName.class })
                .invoke(clazz.getMethod("getPackageManager", new Class[0]).invoke(clazz, null),
                    new Object[] { intent1, intent1.resolveTypeIfNeeded(context.getContentResolver()), Integer.valueOf(65536), intentFilter, Integer.valueOf(1081344), intent1.getComponent() });
} catch (Exception ignore) {
}