12

I want to make it easy to add my app to home screen by pressing a button. So What I am Thinking is a button at the bottom of my app that says "Add to home screen" and when it is pressed, it adds the shortcut to the home screen without closing the application. what code should I add To do that?

CreeperHunter
  • 305
  • 1
  • 3
  • 13

4 Answers4

26

Send an INSTALL_SHORTCUT broadcast with the resulting Intent as an extra (in this case, the result Intent is opening some activity directly).

    //where this is a context (e.g. your current activity)
    final Intent shortcutIntent = new Intent(this, SomeActivity.class);

    final Intent intent = new Intent();
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    // Sets the custom shortcut's title
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
    // Set the custom shortcut icon
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon));
    // add the shortcut
    intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    sendBroadcast(intent);

You also need this permission in your manifest:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
roflharrison
  • 2,300
  • 23
  • 26
  • Thank you for your solution. It works fine for me. But I have another issue. My app supports multiple language. So, when I change the phone language in settings, my app name gets updated fine in launcher. But the app name does not update to new language in home screen. Do I need to add somethings more for that? Thanks for reading.. – Sushil Mar 20 '14 at 00:54
  • Since you cannot modify a shortcut, this will be impossible. – volkersfreunde Apr 23 '14 at 12:04
  • I've noticed a weird thing: when using this code, I always get the "onCreate" method called on the activity of the app when I press the app's icon on the launcher. It occurs even on the case of pressing the home button and then opening the app from the launcher. How come? – android developer Nov 18 '15 at 23:37
  • @androiddeveloper That might be due to the flags that get set for the intent. I'm looking at the 'Shortcut Intents' section [here](https://developer.android.com/reference/android/content/pm/ShortcutManager.html). It says that for static shortcuts, `FLAG_ACTIVITY_NEW_TASK` and `FLAG_ACTIVITY_CLEAR_TASK` will always be set, so "all the existing activities will be destroyed when a static shortcut is launched". If that's your scenario, then that explains the extra `onCreate` since the activity is forced to restart. – Steve Blackwell Dec 16 '16 at 14:53
  • 1
    In Android O (API 26) you can create a `ShortcutInfo` and request it be pinned to the homescreen. There's also a support library method `ShortcutManagerCompat.requestPinShortcut` which calls back to this `com.android.launcher.action.INSTALL_SHORTCUT` broadcast on older platforms. Unfortunately, the doc doesn't mention the manifest permission requirement. https://developer.android.com/reference/android/support/v4/content/pm/ShortcutManagerCompat.html#requestPinShortcut(android.content.Context, android.support.v4.content.pm.ShortcutInfoCompat, android.content.IntentSender) – rockgecko Jul 29 '17 at 14:53
8

Ok ... I know this is old thread but i wanted to make sure engineers visiting this thread have latest information.

Starting from Android O - As part of Background Check Limits ( Implicit receivers in this case ), The com.android.launcher.action.INSTALL_SHORTCUT broadcast no longer has any effect on your app, because it is now a private, implicit broadcast.

Per Android O ActivityManagerService.java :

 case "com.android.launcher.action.INSTALL_SHORTCUT":
                // As of O, we no longer support this broadcasts, even for pre-O apps.
                // Apps should now be using ShortcutManager.pinRequestShortcut().
                Log.w(TAG, "Broadcast " + action
                        + " no longer supported. It will not be delivered.");

I hope this helps !

siva
  • 328
  • 5
  • 7
  • confirming, old broadcast `Intent` way not working on Oreo and above. devs can use [ShortcutManagerCompat](https://developer.android.com/reference/android/support/v4/content/pm/ShortcutManagerCompat.html), when targeting API26 and above (`requestPinShortcut` method contains fallback to `Intent` way on <= API25, above uses `ShortcutManager.pinRequestShortcut()`) – snachmsm Nov 30 '17 at 14:07
5

First step,you should make the luncher could receive a broadcast:

 <!-- Intent received used to install shortcuts from other applications -->
    <receiver
        android:name="com.android.launcher2.InstallShortcutReceiver"
        android:permission="com.android.launcher.permission.INSTALL_SHORTCUT">
        <intent-filter>
            <action android:name="com.android.launcher.action.INSTALL_SHORTCUT"/>
        </intent-filter>
    </receiver>

Next,add a permission in manifest.xml

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>

Finally,create a function and call it when you click the button:

public void createShortCut(){
    // a Intent to create a shortCut
    Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
    //repeat to create is forbidden
    shortcutintent.putExtra("duplicate", false);
    //set the name of shortCut
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcutname));
    //set icon
    Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.icon);
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
    //set the application to lunch when you click the icon
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext() , EnterActivity.class));
    //sendBroadcast,done
    sendBroadcast(shortcutintent);
}

do it like this:

button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            createShortCut();
        }
    });
Laolizi
  • 256
  • 1
  • 2
  • 7
1

For those of us still looking to do this, here is updated code to work with Android O and up:

    ShortcutManager shortcutManager = (ShortcutManager) getSystemService(Context.SHORTCUT_SERVICE);
    if (shortcutManager.isRequestPinShortcutSupported()) {
        ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(getApplicationContext(), "1")
                .setIntent(new Intent(getApplicationContext(), DestionationAcitivity.class).setAction(Intent.ACTION_MAIN))
                .setShortLabel("My Shortcut")
                .setIcon(Icon.createWithResource(this, R.drawable.yourDrawable))
                .build();
        shortcutManager.requestPinShortcut( shortcutInfo, null);
    } else {
        Toast.makeText(MainActivity.this,"Creating Shortcuts is not Supported on this Launcher",Toast.LENGTH_SHORT).show();
    }
Pythogen
  • 591
  • 5
  • 25