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?


Solution 1:

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.

Solution 2:

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.

Solution 3:

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" />

Solution 4:

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