14

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?

Kara
  • 6,115
  • 16
  • 50
  • 57
wikichen
  • 2,253
  • 3
  • 18
  • 28
  • 3
    Are you talking about non-Honeycomb devices? And with "desktop", do you mean the launcher? I've never seen that before, and I hope I never will, because that sounds like overly intrusive behavior. If I want something in the launcher, I'll put it there myself. In general though, it's not possible to do anything upon installation, your code is only executed once you run it. You do have the option have one or more icons in the list of apps, or none. That's done in the manifest. – EboMike Jul 19 '11 at 22:46
  • No, this is for a Honeycomb app that I'm developing. I notice for apps like SoundHound and Google Body, a small modal pops up on the bottom of the screen notifying me that a shortcut has been created after I receive the notification that the installation is successful. I realize this may not be ideal for the user, but my client is making the calls, so I'm merely looking for a feasible implementation. – wikichen Jul 19 '11 at 23:03

4 Answers4

15

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.

Dan S
  • 9,139
  • 3
  • 37
  • 48
  • 1
    Can you give more concrete examples for how to implement this? I'm pretty new to Android dev and any guidance would be great. – wikichen Jul 19 '11 at 23:07
  • All the links to android.git.kernel.org are dead. – 1615903 Mar 30 '15 at 06:11
  • The answer is historical for Launcher2. [Launcher3](https://android.googlesource.com/platform/packages/apps/Launcher3/) is the current version and the links have been updated to the current websites. – Dan S Mar 31 '15 at 08:16
5

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.

wikichen
  • 2,253
  • 3
  • 18
  • 28
  • 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 – Zoombie Oct 24 '11 at 07:41
  • 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). – Wytze Feb 22 '12 at 15:56
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);
}
Robert
  • 1,660
  • 22
  • 39
  • This works like charm, but adds another icon on startup... any way of doing it only once? upon installation and not in `onCreate()`? – Omri374 Jul 23 '14 at 07:01
  • 1
    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. – Robert Jul 24 '14 at 12:24
  • Use the `ACTION_PACKAGE_FIRST_LAUNCH` intent filter. – Bill Mote May 05 '17 at 16:58
3

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" />
mike_m
  • 1,526
  • 4
  • 14
  • 19
Vaishali Sutariya
  • 5,093
  • 30
  • 32