portfoliobuilder portfoliobuilder - 3 months ago 70
Android Question

How to set default app launcher programmatically?

I am creating a launcher (kiosk) app that will be downloadable through google. When first installing this application the user has the ability of choosing which launcher (mine or the stock) will be the default. I am trying to bring this up manually if the user does not make my application the default launcher. I want the user to be forced into selecting ALWAYS instead of JUST ONCE when that dialog comes up, otherwise the dialog will continue to appear periodically with a friendly message. This is what I have attempted so far.

I created a method to check for if my application is the default

/**
* method checks to see if app is currently set as default launcher
* @return boolean true means currently set as default, otherwise false
*/
private boolean isMyAppLauncherDefault() {
final IntentFilter filter = new IntentFilter(Intent.ACTION_MAIN);
filter.addCategory(Intent.CATEGORY_HOME);

List<IntentFilter> filters = new ArrayList<IntentFilter>();
filters.add(filter);

final String myPackageName = getPackageName();
List<ComponentName> activities = new ArrayList<ComponentName>();
final PackageManager packageManager = (PackageManager) getPackageManager();

packageManager.getPreferredActivities(filters, activities, null);

for (ComponentName activity : activities) {
if (myPackageName.equals(activity.getPackageName())) {
return true;
}
}
return false;
}


Then I make the attempt of launching the chooser

/**
* method starts an intent that will bring up a prompt for the user
* to select their default launcher. It comes up each time it is
* detected that our app is not the default launcher
*/
private void launchAppChooser() {
Log.d(TAG, "launchAppChooser()");
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}


When I do this I am not receiving the choice between my app and the stock launcher. I tried using
startActivity(Intent.createChooser(intent, "Please set launcher settings to ALWAYS"));
and I get the choices between my app and the stock launcher, however, I don't get the options ALWAYS or JUST ONCE.

I can create a custom dialog for this instead of launching chooser but I need to know how to set the default app launcher programmatically. Thanks in advance!

Answer

This is actually possible with a little workaround:

Create an empty Activity that acts as a launcher called FakeLauncherActivity. Add it to your manifest as a disabled component:

<activity
    android:name="com.path.to.your.FakeLauncherActivity"
    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>

Check whether your desired launcher activity is the default one (with the isMyAppLauncherDefault() from your question).

If not, offer the user to choose the preferred launcher activity like this:

public static void resetPreferredLauncherAndOpenChooser(Context context) {
    PackageManager packageManager = context.getPackageManager();
    ComponentName componentName = new ComponentName(context, com.path.to.your.FakeLauncherActivity.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);
}

This method temporarily enables FakeLauncherActivity, which leads to a change in the set of available launcher activities, which forces Android to forget its default launcher. You will see something like...

521-735/system_process I/PackageManager´╣Ľ Result set changed, dropping preferred activity for Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10000000 } type null

... in your log.

The method then simply opens a launcher intent where you can see all installed launchers and the buttons "Always" / "Just once". Finally, the method disables FakeLauncherActivity again so that it doesn't display in the list.

You could repeat that as often as you want and only let the user proceed if your desired launcher activity is set as default.