7

Possible Duplicate:
Android create shortcuts on the home screen

I am having one TextView and Button in my Activity (HomeActivity.class).

Goal: When I click on the Button it should create a shortcut with the default android image as icon and text as entered in the TextView.

So far I've found out that we can create shortcuts using ACTION_CREATE_SHORTCUT

This is the code I have so far:

Intent result = new Intent(android.content.Intent.ACTION_CREATE_SHORTCUT);
result.putExtra(Intent.EXTRA_SHORTCUT_INTENT,
                new Intent(getApplicationContext(), Editor.class));
result.putExtra(TodoDbAdapter.KEY_ROWID,rowid);
result.putExtra(Intent.EXTRA_SHORTCUT_NAME,textView.getText());
result.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                Intent.ShortcutIconResource.fromContext(HomeActivity.this,
                                                        R.drawable.icon));
setResult(RESULT_OK, result);

My Manifest File :

<activity android:name=".HomeActivity" android:label="@string/app_name">

        <intent-filter>
                <action android:name="android.intent.action.CREATE_SHORTCUT" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

But this code does not create any shortcut.

How can I create shortcuts?

Community
  • 1
  • 1
Kartik Domadiya
  • 29,868
  • 19
  • 93
  • 104
  • 1
    I think you should have a look at [this page](http://codinggeek.org/2011/01/02/android-how-to-add-home-screen-shortcuts-to-your-app/). It's also possibly a duplicate of [this question](http://stackoverflow.com/questions/5676090/android-is-there-a-programming-way-to-create-a-web-shortcut-on-home-screen) or [this question](http://stackoverflow.com/questions/6337431/android-create-shortcuts-on-the-home-screen). – N-JOY Jun 24 '11 at 04:52

1 Answers1

18

Try this:

Intent shortcutIntent;
shortcutIntent = new Intent();
shortcutIntent.setComponent(new ComponentName(activity.getPackageName(), ".classname"));

shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

final Intent putShortCutIntent = new Intent();
putShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);

// Sets the custom shortcut's title
putShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,"Title");
putShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(PersonProfile.this, R.drawable.icon));
putShortCutIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(putShortCutIntent);

and add this permission in manifest:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
Kamran Ahmed
  • 7,661
  • 4
  • 30
  • 55
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
  • Doesn't work for me. Does this still work ? – toto_tata Jun 20 '13 at 16:33
  • @CapDroid: This creates a shortcut even if there exists one. Can we create the shortcut in a way that it replaces the existing one instead of duplicating..? Please guide. – Mudassir Nov 13 '14 at 04:00
  • @CapDroid: Isn't this just for the stock launcher? Or any launcher set as default by the user? – Kamran Ahmed Mar 06 '15 at 17:23
  • 2
    This won't work on Android 8.0 and above. Instead you need to use this: `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)` – android developer Apr 24 '18 at 19:05