11

When a user installs an Android app, a launcher icon is created in the apps menu. Many users I talk to expect that when they install an app, an icon should appear automatically on their home screen ("launch pad").

A lot of apps achieve this somehow. My preference would be to have a window appear on install asking the user "Do you want to add a shortcut?" If that's not possible, any code that auto-adds the shortcut will do.

Android gives a bunch of code here: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/LauncherShortcuts.html It is implied that adding this code (and the related xml) to your project will do the trick. But it does not have the effect I want. It seems the code provided is passive, and I need to trigger it somehow.

So my question is:

How do I trigger the installation of a shortcut, and how do I make sure it happens only once, preferably triggered by some kind of "app install" event?

PS: A complicating factor is that I am building my app using PhoneGap, meaning the main activity is not "Activity" but "DroidGap".

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Wytze
  • 7,844
  • 8
  • 49
  • 62
  • 1
    Turns out an app installed through Android Market will install a launcher shortcut automatically if you include the INSTALL_SHORTCUT permission. But you can't tell until you've published your app: even installing from an APK you download to your phone via other means won't do the trick. – Wytze Mar 16 '12 at 14:39

3 Answers3

5
    Intent shortcutIntent = new Intent(getApplicationContext(), HomeScreen.class);      
    shortcutIntent.setAction(Intent.ACTION_MAIN);

    Intent addIntent = new Intent();
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "AIMS ICD");
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.aims));
    addIntent.putExtra("duplicate", false);
    addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    getApplicationContext().sendBroadcast(addIntent);
Marek Sebera
  • 39,650
  • 37
  • 158
  • 244
user2002721
  • 159
  • 3
  • 9
1

In the example, it returns the intent in setResult(...). I believe you need to run sendBroadcast(intent) to trigger installation of the shortcut.

triad
  • 20,407
  • 13
  • 45
  • 50
  • Triad, thanks for this comment. I have tried running sendBroadcast(intent) from different places in both App.java (the main activity) and in LauncherShortcuts.java itself. No result. – Wytze Feb 27 '12 at 09:29
  • Hm do you also have `` in your `AndroidManifest.xml`? – triad Feb 27 '12 at 09:38
  • I also noticed the sample code doesn't call `intent.setAction(Intent.ACTION_CREATE_SHORTCUT)`, which is also necessary. – triad Feb 27 '12 at 09:45
  • Hii @triad i implemented the same and successfully created the icon now I am not able to click to launch application using short cut any suggestion would be appreciated .? – Shani Goriwal Mar 02 '15 at 12:46
0

The class DroidGap extends Activity so you can just add in the code from the link you provided to add a shortcut.

Simon MacDonald
  • 23,253
  • 5
  • 58
  • 74
  • Simon, I tried adding the code from the link. That didn't work. I included LauncherShortcuts.java and LauncherShortcuts.xml, but nothing happens when I install my app. Perhaps you have a more specific idea than I do when you say "add in the code from the link"? – Wytze Feb 27 '12 at 09:29