How do I get the default launch start prompt?

I want to determine if my launcher application is the default launcher, and if you do not prompt, the user selects my application as the default launcher. The problem I am facing is that the prompt appears without the options "Just Once" and "Always". Also, when choosing launcher in my application, the default value is not set.

In my onCreate, I have the following check to see if my application is a standard launcher, and then I launch a dialog with the intention that the user can select my application 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 of checking if my application is a standard 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;   
}

. (. ). . .

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

ALWAYS, . !

+4
1

, , , "Just Once" "Always".

createChooser() - , . createChooser() Intent. , " " "".

, , , Intent . , "" .

+6

Source: https://habr.com/ru/post/1570053/


All Articles