9

I want to create a shortcut in an android app, it lead to another activity which is not launcher of the app.

Yunfei Tang
  • 496
  • 2
  • 6
  • 11

2 Answers2

20

To create the shortcut itself you need a specially crafted activity which must:

  • Be defined in your AndroidManifest.xml with an intent filter with the action android.intent.action.CREATE_SHORTCUT.
  • Return a result, an Intent, containing your actual shortcut. The shortcut itself is represented by another Intent.

This activity will then show up when you longpress your desktop and select "Shortcuts".

Of course the shortcut by itself is not much use, so you must add an intent filter to whatever activity you want to get triggered by the shortcut. The intent filter should match whatever Intent you chose for your shortcut.

I wrote a small how-to on the subject, it's got more details: http://www.kind-kristiansen.no/2010/android-adding-desktop-shortcut-support-to-your-app/

Do tell me if anything is unclear in that post, I'll try to clear it up.

rogerkk
  • 5,494
  • 5
  • 37
  • 53
  • @rogerkk, please help me with my question. http://stackoverflow.com/questions/21371491/create-app-shortcut-inside-the-custom-launcher-in-android – androidBoomer Jan 27 '14 at 02:53
  • Sorry @androidBoomer, I haven't really done any Android development lately so I'm probably not the right person to ask at the moment. :) – rogerkk Jan 28 '14 at 07:46
  • Is there a way to create the shortcut in the home screen directly from inside the app? I have an app that is feed with data through `getData()`, and the user can have several icons to display each. The idea is that the user creates his own shortcuts with the app with his own data (in particular, he cannot edit the manifest as your method requires). Thanks! – Luis A. Florit Apr 14 '14 at 17:37
  • Can anyone tell me how disable creating duplicate shortcut that are created using the method above. – Mayank Jun 04 '14 at 10:47
  • Thank you so much, i cant find anything on google on this topic – Hamzah Malik Nov 01 '14 at 15:16
  • My activity is not getting called by launcher can anyone help me? – Manikanta Mar 21 '17 at 11:39
  • For complete code look at this: http://easybook4u.com/index.php/2017/09/11/how-to-create-a-shortcut-for-non-launcher-activity/ – Himanshu arora Sep 11 '17 at 17:20
1

I have developed one method below for creating the shortcut icon on android Homescreen. Just call it.

private void ShortcutIcon(){

    Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
    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, "Test");
    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);
}

Don't forget to change your activity name, icon resource . Happy coding !!!

Siddiq Abu Bakkar
  • 1,949
  • 1
  • 13
  • 24