20

I know it's not documented and won't work on every device, but I see more and more apps placing their shortcuts on the home screen after they got installed. Found bunch of code chunks how to do it but for me they don't fit together. This is what I got for now.

  1. Need a permission in the manifest.

    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
    
  2. Create an Intent of activity that should be called. Ex (from cgeek):

    Intent shortcutIntent = new Intent();
    shortcutIntent.setClassName("com.example.androidapp", "SampleIntent");
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    
  3. Create shortcut itself

    Intent addIntent = new Intent();
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcut Name");
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.icon));
    addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    context.sendBroadcast(addIntent);
    

My questions is: Where this code should go to make shortcut added after .apk installed? I tried this code in the launcher activity, it creates broken(another story) shortcut every time app starts.

2
  • I also agree as a user, it is really annoying. But... As a developer, if it is requested and if i explained that it must not be done but still insisting me to do that. Yeah, you can do that after ICS. Permission is necessary, for the rest check this answer
    – yahya
    Nov 11, 2013 at 12:14
  • THis answer is not working on kitkat...
    – Noman
    Nov 5, 2014 at 10:17

4 Answers 4

20

As far as I know, that's an optional feature of the Market app, not of the apps themselves. By design an application does not receive a broadcast about itself being installed. If that codes works, the soonest you can execute it is the first time the user launches the app. That said:

Do. Not. Automatically. Create. App. Shortcuts.

Ever.

Don't usurp the user's UI design.

8
  • 4
    "Do. Not. Automatically. Create. App. Shortcuts." I disagree. Viber do that and Viber is not so evil like that. This is useful because the target people of Viber do not is nerd or geek. They don't know how to manage the home screen. It's a fact.
    – Felipe
    Nov 2, 2011 at 0:25
  • 6
    1 special case in the ocean of apps is not enough to disregard a fairly basic and uncontroversial rule of thumb. If a user doesn't understand how to manage their home screen, it's all the more reason to NOT put something there that they don't know how to remove. Nov 6, 2011 at 18:38
  • IMHO it's convenient to see app's shortcut, which I wanted to install and try to use right away on the screen, than spend couple+ seconds searching for it among all apps. People that can't manage basic things on android usually do not use those devices. No offence, but your arguments are unconvincing.
    – Maxim
    Dec 12, 2011 at 14:19
  • 7
    Really tired of people thinking they know everything better than others saying "Do not do this, do not do that, this is evil".
    – Alex
    Nov 8, 2012 at 0:29
  • 4
    The point is that Android is an OS not only used on smartphones or tablets (there are thousands of devices!). There are other contexts where you develop apps for Android, sometimes not even using the market at all. One can have VERY GOOD reasons to do what you could call evil in a usual case. And this also applies to the Android dev team: stating that something shall never be done in usual cases is not an excuse to limit the API on that specific point. This just goes against the flexibility we except from a good API & OS.
    – Alex
    Nov 8, 2012 at 0:44
4

I agree with the currently accepted answer, that you should not do this by receiving a broadcast or at-install time. Don't do anything without user interaction or permission.

However, if you provide a button in your application, you would put this in the buttons OnClick handler. It would then add a shortcut when the user selects the "add shortcut" option.

2

This can be possible just add the below code to your main activity in oncreate method

        Intent HomeScreenShortCut= new Intent(getApplicationContext(),
                MainActivity.class);

        HomeScreenShortCut.setAction(Intent.ACTION_MAIN);
        HomeScreenShortCut.putExtra("duplicate", false);
        //shortcutIntent is added with addIntent
        Intent addIntent = new Intent();
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, HomeScreenShortCut);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "AppName");
        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);

Add add this permission to your manifest

 <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
1
  • It adds the shortcut, but if you try to open the app using that it sends a toast "App isn't installed". Am I missing something here? Thank you.
    – Gabi Radu
    Jul 9, 2015 at 8:58
0

I used this code to create 3 shortcuts on homepage:

Intent HomeScreenShortCut= new Intent(getApplicationContext(),
            MainActivity.class);

HomeScreenShortCut.setAction(Intent.ACTION_MAIN);
HomeScreenShortCut.putExtra("duplicate", false);
//shortcutIntent is added with addIntent
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, HomeScreenShortCut);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "AppName");
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);
4
  • What's the "duplicate" for? Mar 1, 2015 at 20:25
  • @androiddeveloper it is in order to avoid duplicating icons (it doesnt create an icon if it already exists in home screen). Jul 21, 2015 at 13:10
  • I didn't know about this. Where did you find about this parameter? Do you know of others? Jul 21, 2015 at 18:53
  • say, what's the correct way to put a shortcut with an icon of another app? You can't obviously use ShortcutIconResource... Jul 26, 2015 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.