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.

laalto
  • 150,114
  • 66
  • 286
  • 303
Maxim
  • 4,152
  • 8
  • 50
  • 77
  • 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](http://stackoverflow.com/a/18327304/1171484) – yahya Nov 11 '13 at 12:14
  • THis answer is not working on kitkat... – Noman Nov 05 '14 at 10:17

4 Answers4

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.

LeffelMania
  • 12,765
  • 4
  • 32
  • 34
  • 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 02 '11 at 00: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. – LeffelMania Nov 06 '11 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 '11 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 08 '12 at 00: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 08 '12 at 00:44
  • I agree completely with Alex's comment...IMHO it is very narrow minded to only think of Android as a phone OS. – luckyreed76 Dec 12 '13 at 17:52
  • Looks like it's not allowed anymore : "Apps and their ads must not modify or add browser settings or bookmarks, add homescreen shortcuts, or icons on the user’s device as a service to third parties or for advertising purposes. " but I am not sure, because of the last sentence. (from the new policy: http://play.google.com/intl/en/about/developer-content-policy.html) – Samuel Mar 31 '14 at 07:31
  • can be done see this http://stackoverflow.com/questions/16873256/how-to-add-shortcut-to-home-screen-in-android-programatically – Syed Raza Mehdi Oct 16 '14 at 07:04
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.

HaMMeReD
  • 2,440
  • 22
  • 29
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" />
  • 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 09 '15 at 08: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);
Gnucki
  • 5,043
  • 2
  • 29
  • 44