简体   繁体   中英

How can I place app icon on launcher home screen?

As you know, when app is nomally installed, icon is created at launcher menu screen. What I want to do is create icon at user home screen during installation. (without pressing icon for 5 seconds.)

I heard this from another source to just add

<category android:value="android.intent.category.HOME" />

to AndroidManifest.xml file, but it didn't work.

Is there any other way to do it?

You can use this:

Intent shortcutIntent = new Intent();
shortcutIntent.setClassName("packageName", "className");
//shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "shortcut_name");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(context, R.drawable.icon));
//intent.putExtra("duplicate", false);
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
            context.sendBroadcast(addIntent);

You have to use following permission in your AndroidManaifest.xml

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

You can use the commented code according to your requirements.

Note that, perhaps, above API is not documented. But it works.

This is now solved by the Google Play services. You don't have to add any codes to do it anymore. Now when you install an app from the Google Play Store it automatically creates the logo in the main screen. It can be handled in the Google Play store settings. Exception : If you are using any custom roms or launchers, it does not work with some.

I use these methods to properly add or remove shortcuts. These methods are working pretty well and are the same as the Android System when the user manually add/remove a shortcut .

public static void addShortcut(Context context)
{
    Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");

    ApplicationInfo appInfo = context.getApplicationInfo();

    // Shortcut name
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, appInfo.name);
    shortcut.putExtra("duplicate", false); // Just create once

    // Setup activity shoud be shortcut object 
    ComponentName component = new ComponentName(appInfo.packageName, appInfo.className);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(component));

    // Set shortcut icon
    ShortcutIconResource iconResource = Intent.ShortcutIconResource.fromContext(context, appInfo.icon);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);

    context.sendBroadcast(shortcut);
}

public static void deleteShortcut(Context context)
{
    Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");

    ApplicationInfo appInfo = context.getApplicationInfo();

    // Shortcut name
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, appInfo.name);

    ComponentName comp = new ComponentName(appInfo.packageName, appInfo.className);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));

    context.sendBroadcast(shortcut);
}

Permissions :

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />

I had trouble with the above answers and that's because API 22 and maybe other APIs returns on getApplicationInfo().className in debug/emulation mode:
"com.android.tools.fd.runtime.BootstrapApplication"

Most important thing is that I needed to set the entire path to the class for the class name. (see the changes)

For example, I had the following:
packageName=com.example.user.myapp
className=MainActivity

In Kailash answer I needed to change this line:

shortcutIntent.setClassName("packageName", "className");

to

shortcutIntent.setClassName("com.example.user.myapp", "com.example.user.myapp.MainActivity");



In ChristopheCVBs answer I needed to change this line:

ComponentName comp = new ComponentName(appInfo.packageName, appInfo.className);

to

ComponentName comp = new ComponentName(appInfo.packageName, appInfo.packageName+".MainActivity");

Robust Solution for all devices (Both <26 or >26 ).

createShortcutOnHome(CurrentActivity.this, ActivityToOpen.class, "app name", R.mipmap.ic_launcher);

This method can be placed in your Util.

 public static void createShortcutOnHome(@NonNull Activity activity, Class activityToOpen, String title, @DrawableRes int icon) {
        Intent shortcutIntent = new Intent(activity, activityToOpen);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { // code for adding shortcut on pre oreo device 
            Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
            intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
            intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
            intent.putExtra("duplicate", false);
            Parcelable parcelable = Intent.ShortcutIconResource.fromContext(activity, icon);
            intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, parcelable);
            activity.sendBroadcast(intent);
            System.out.println("added_to_homescreen");
        } else { 
            ShortcutManager shortcutManager = activity.getSystemService(ShortcutManager.class);
            assert shortcutManager != null;
            if (shortcutManager.isRequestPinShortcutSupported()) {
                ShortcutInfo pinShortcutInfo =
                        new ShortcutInfo.Builder(activity, "browser-shortcut-")
                                .setIntent(shortcutIntent)
                                .setIcon(Icon.createWithResource(activity, icon))
                                .setShortLabel(title)
                                .build();

                shortcutManager.requestPinShortcut(pinShortcutInfo, null);
                System.out.println("added_to_homescreen");
            } else {
                System.out.println("failed_to_add");
            }
        }
    }

You could make a setup function that the user can use to do all potential actions. For example, make any necessary user-accessible folders(if they don't exist already), adding the shortcut to the home screen, and/or loading in initial data.

This is a function I'm actually writing now, so hopefully it turns out well. But yeah the point is to keep it out of the onCreate() so it doesn't get called every single time!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM