«

»

Nov 21

Android: Adding desktop shortcut support to your app

So you have an app with quite a bit of functionality, but you would like to offer your users some way of performing often used actions or go directly to an often used activity besides the one triggered by your apps launcher. Android offers desktop shortcuts (if you don’t know about them, try long-pressing your Android desktop) for this situation, but how do you actually implement support for this in your app?

I was not able to find any straight forward documentation or how-to on this subject, so hopefully this writeup will be helpful to someone.

The short story is that 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.

So let’s take this step by step:

1. Creating the activity

Nothing special here. When adding your shortcut from your Android desktop this Activity will get started and act as any other Activity. Just create the activity and override onCreate().

2. Configuring the manifest

Add your activity like any other, to the <application> section. Add the following intent filter inside the definition of that activity:

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

Already you should be able to see your activity show up among the other shortcuts if you longpress your Android desktop and select “Shortcuts” from the menu that pops up.

3. Implement your shortcut activity’s onCreate() to return the shortcut.

This activity can do pretty much anything you want. You can for example have it pop up a list of actions to choose from. I’ll explain the simplest possible version here, for clarity.

The trick here is that you should use the result of your activity to return the shortcut you wish to add to the users Android desktop. The result is set using setResult() (see Activity API docs for details), which returns an Intent. The Intent should containt the following:

  • Another intent, which is the actual shortcut itself. This intent will be triggered when the user presses your shortcut icon.
  • The actual name of your shortcut, as you want it displayed on the users desktop
  • An icon resource to be displayed on the desktop.

The Activity will then look somewhat like this:

public class ShortcutActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// The meat of our shortcut
		Intent shortcutIntent = new Intent("com.test.yourapp.SOME_INTENT");
		ShorthcutIconResource iconResource = Intent.ShortcutIconResource.fromContext(this, R.drawable.launcher_icon);
        
        // The result we are passing back from this activity
        Intent intent = new Intent();
        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcut Test");
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
        setResult(RESULT_OK, intent);
        
        finish(); // Must call finish for result to be returned immediately
	}

}

And that’s it. If you followed these steps you should now have a fully functional Android desktop shortcut.

6 comments

Skip to comment form

  1. DEiE

    Thanks for your explanation, just the information I needed to create a shortcut for my app!

  2. Source hunter

    Now if only you’d show us how to do that in Basic for Android and we’d all be happy :)

  3. Juan Elosua

    Hello,

    I’m trying to create a desktop shortcut from inside the execution of an activity of my own app, but I am having problems…

    What I want to do is the same whatsup app does when you long click on a specific conversation…

    This is the code I’m trying to use once the user long clicks and selects the create shortcut option:
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    TextView tv = (TextView)info.targetView.findViewById(R.id.bstop_id);
    String stopId = tv.getText().toString();

    Intent shortcut = new Intent (Intent.ACTION_MAIN);
    shortcut.setClassName(this, TimetableAct.class.getName());
    shortcut.putExtra(BookmarksDbAdapter.KEY_STOPID, stopId);

    Intent intent = new Intent();
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcut);
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, stopId);
    Parcelable iconResource = Intent.ShortcutIconResource.fromContext(BookmarkAct.this, R.drawable.ic_bstop);
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
    intent.setAction(Intent.ACTION_CREATE_SHORTCUT);
    getApplicationContext().sendBroadcast(intent);

    But it is not working….

    Any help will be really appreciated.

    Thanks in advance.

    Juan

  4. Simon

    Excellent how-to. Just what I was looking for. Thank you for spending the time to write this up.

  5. Gloria

    This is an old post but here is some working code that might help Juan:

    Intent shortcutIntent = new Intent(context, com.face.me.app.card.MyActivity.class);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    shortcutIntent.putExtra(“Shortcut”, “Shortcut”); //I added this because I want MyActivity to act differently when launched from the shortcut – I check the intent’s extras

    //Create the shortcut itself
    Intent addIntent = new Intent();
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, context.getString(R.string.business_card_shortcut_label));
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.ic_launcher_icon));
    addIntent.setAction(“com.android.launcher.action.INSTALL_SHORTCUT”);
    context.sendBroadcast(addIntent);

  6. http://roysdonludaf.edublogs.org

    I blog often and I seriously appreciate your content. This great article has truly peaked my interest.

    I am going to book mark your blog and keep checking for new
    details about once a week. I subscribed to your RSS feed as well.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>