How to enable automatic shortcut creation after app downloads and installs from Android Market?

22,400

Solution 1

Send an intent to the Launcher. Broadcast a INSTALL_SHORTCUT intent with a EXTRA_SHORTCUT_NAME and EXTRA_SHORTCUT_INTENT. The extra EXTRA_SHORTCUT_DUPLICATE can be used to help manage duplicate shortcuts being made. The details can be found in the Launcher2 project in the AOSP repository.

Please be careful with this, some users may not appreciate having a shortcut created without permission.

Here is some pseudo code:

Intent installIntent = new Intent(InstallShortcutReceiver.ACTION_CREATE_SHORTCUT);
Intent myAppIntent = new Intent(Context.getPackageContext(), MyActivity.class);
installIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, Context.getString(R.string.app_name));
installIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myAppIntent);
installIntent.putExtra(Intent.SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.icon));
Context.sendBroadcast(installIntent);

There is some more information in the Intent class as well as an alternate Intent action.

Solution 2

It turns out the Android Market automatically installs a desktop shortcut when you download an app. I'm noticing this default behavior on an Android 3.1 tablet with a Honeycomb (3.0+) targeted app. It seems like this isn't standard convention for apps running on previous Android versions, where explicit user input/permission is needed upon first downloading/launching the app.

Solution 3

I've tested this demo. And put the addShortcut() in the onCreate(). Then deploy from intelliJ, my GEL(Google Experience Launcher) do create shortcut for the first launch. I afraid it keep creating shortcut, but it seem won't create another shortcut if app launch next time. Try for yourself if you needed.

private void addShortcut() {
    //Adding shortcut for MainActivity
    //on Home screen
    Intent shortcutIntent = new Intent(getApplicationContext(),
            MainActivity.class);

    shortcutIntent.setAction(Intent.ACTION_MAIN);

    Intent addIntent = new Intent();
    addIntent
            .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
            Intent.ShortcutIconResource.fromContext(getApplicationContext(),
                    R.drawable.ic_launcher));

    addIntent
            .setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    getApplicationContext().sendBroadcast(addIntent);
}

Solution 4

adding shortcuts

private void addShortcut() {

    //Adding shortcut for MainActivity 
    //on Home screen
    Intent shortcutIntent = new Intent(getApplicationContext(),
            MainActivity.class);

    shortcutIntent.setAction(Intent.ACTION_MAIN);

    Intent addIntent = new Intent();
    addIntent
            .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "ApplicationName");
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
            Intent.ShortcutIconResource.fromContext(getApplicationContext(),
                    R.drawable.ic_launcher));

    addIntent
            .setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    getApplicationContext().sendBroadcast(addIntent);
}

for removing shortcut

private void removeShortcut() {

    //Deleting shortcut for MainActivity 
    //on Home screen
    Intent shortcutIntent = new Intent(getApplicationContext(),
            MainActivity.class);
    shortcutIntent.setAction(Intent.ACTION_MAIN);

    Intent addIntent = new Intent();
    addIntent
            .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "ApplicationName");

    addIntent
            .setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
    getApplicationContext().sendBroadcast(addIntent);
}

permissions

<uses-permission
    android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission
    android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
Share:
22,400
wikichen
Author by

wikichen

Code and design.

Updated on March 31, 2020

Comments

  • wikichen
    wikichen over 4 years

    I notice that some apps on the Android Market automatically create shortcuts on the desktop after downloading and installing onto your device, while some don't. How would I go about implementing this behavior?

  • wikichen
    wikichen almost 13 years
    Can you give more concrete examples for how to implement this? I'm pretty new to Android dev and any guidance would be great.
  • Zoombie
    Zoombie over 12 years
    Yes i have experienced same thing, application downloaded from android market, on android 3.1 tablet creates shortcut on homescreen, but with android 2.3 it does not create any shortcut on homescreen, i have tried on android 2.3 phone... but is it mentioned anywhere on "Android site" or in any blog that by default app shortcut will be created on homescreen of android 3.0 + higher version tablets
  • Wytze
    Wytze over 12 years
    In my experience with Android 2.3, which then upgraded tot 3.0, 3.1 and 4.0.3, there were only a few apps that automatically got a place on my screen (I don't like to call it desktop or launcher).
  • Omri374
    Omri374 almost 10 years
    This works like charm, but adds another icon on startup... any way of doing it only once? upon installation and not in onCreate()?
  • Robert
    Robert almost 10 years
    You could use a preference when first launch after installation. Check if it's null then it's the first launch then go to addShortcut(). Remember to give it a value afterward to escape the if-else.
  • 1615903
    1615903 over 9 years
    All the links to android.git.kernel.org are dead.
  • Dan S
    Dan S over 9 years
    The answer is historical for Launcher2. Launcher3 is the current version and the links have been updated to the current websites.
  • Bill Mote
    Bill Mote about 7 years
    Use the ACTION_PACKAGE_FIRST_LAUNCH intent filter.