2

I want to give the user the option of add a homescreen shortcut to launch the app (prompting him at the first launch).

I saw that for example Whatsapp create that icon automatically

How can I do it? Does the app require additional permissions? Thanks

Im trying with (but doesn't work)

    Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");

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

    // Setup current activity shoud be shortcut object 
    ComponentName comp = new ComponentName(this.getPackageName(), "."+this.getLocalClassName());  
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));  

    // Set shortcut icon
    ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher);  
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);  

    sendBroadcast(shortcut);  
Addev
  • 31,819
  • 51
  • 183
  • 302

1 Answers1

4

Did you set the activity intent in your manifest file? read here:

Android create shortcuts on the home screen

Community
  • 1
  • 1
Ozzy
  • 8,244
  • 7
  • 55
  • 95
  • You should rather not allow it to create the shortcut again if its already there. Use the shared prefs file to make a note in your app if the short cut was installed or not, if it was installed don't create another one. – Ozzy Dec 17 '11 at 18:39
  • Okey I checked (clearing user data) and WhatsApp also uses that system.Thanks! – Addev Dec 17 '11 at 18:43