Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bring up the set default launcher prompt?

I want to detect if my launcher app is the default launcher, and if not bring up a prompt to have the user choose my app as the default launcher. The issue I am facing is that the prompt comes up without the options "Just Once" and "Always". Additionally, selecting on my launcher app does not set the default.

In my onCreate I have the following check to see if my app is the default launcher, and then I launch a dialog with intent so that user can choose my app as the default launcher.

    if (!isMyAppLauncherDefault()) {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        startActivity(Intent.createChooser(intent, "Set as default to enable Kiosk Mode"));
    }

Here is my method for checking if my app is the default launcher

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

    // You can use name of your package here as third argument
    packageManager.getPreferredActivities(filters, activities, null);

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

Note: I have tried changing how I launch my intent (see code snippet below). But then the launcher does not come up at all. Nothing happens.

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

How can I get the ALWAYS button to display, and how can I set the actual default launcher settings. Thank you in advance!

like image 767
portfoliobuilder Avatar asked Jan 05 '15 19:01

portfoliobuilder


1 Answers

The issue I am facing is that the prompt comes up without the options "Just Once" and "Always".

The createChooser() is causing that -- that is designed for a one-off chooser, with no option for making a default. Remove your createChooser() call, and just use the Intent that you constructed. If there is no default launcher, the user will get the standard chooser with the "Just Once" and "Always" options.

Note, though, that if another launcher is the default, then your Intent will just route to that launcher. There is nothing you can really do in this case, other than to provide guidance to the user to go into Settings and remove their default association for that app.

like image 186
CommonsWare Avatar answered Oct 08 '22 21:10

CommonsWare