Tuesday, May 17, 2011

PROVIDING LAUNCHER SHORTCUT – Android

In this article we'll discuss how to provide facility to add shortcut to your activity to the launcher
Contents:
Adding shortcut to launcher
An activity actually handles two stages of launcher shortcut's life cycle.
1. Activity must provide the shortcuts to the launcher. That is, when the user installs a shortcut, your activity must generate the actual shortcut and returns it to the launcher along with the shortcut name and icon which will be displayed to user.
2. Whenever user clicks on your applications icon, intent is sent. Your activity typically handles this intent as necessary

Now let’s see how we can handle the stage 1.
Your application simply creates an "Intent" that the launcher will use to create the shortcut.A more interactive way such as a User interface can be provided to user to make a more customizable shortcut.For example, Your User Interface may provide the way to select the specific nature of the shortcut, i.e. contact, picture, URL, media item etc.

//This function creates a shortcut and returns it to the caller. There are actually two
//intents that you will send back.
private void setupShortcut() {
// First, set up the shortcut intent. For this example, we simply create an intent that
// will bring us directly back to this activity. A more typical implementation would use a
// data Uri in order to display a more specific result, or a custom action in order to
// launch a specific operation.

Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
shortcutIntent.setClassName(this, this.getClass().getName());
shortcutIntent.putExtra(SHTCT_KEY, "Example Shortcut provided");

// Then, set up the container intent (the response to the caller)

Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Name");
Parcelable iconResource = Intent.ShortcutIconResource.fromContext(
this, R.drawable.icon);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);

// Now, return the result to the launcher

setResult(RESULT_OK, intent);
}

This function provides the Intent that will be returned to launcher in the container Intent .

Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
shortcutIntent.setClassName(this, this.getClass().getName());
shortcutIntent.putExtra(SHTCT_KEY, "Example Shortcut provided");

This is the intent that will be sent to your activity when user clicks Shortcut Icon.
First we are setting up the shortcut intent. Here in this example we are telling launcher that we need to handle “ACTION_MAIN” Action i.e. this intent will bring us directly back to this activity. However you can use data Uri with Action as “ACTION_VIEW” to display more specific result, or, a custom action to launch a specific operation provided your Activity handles that Action which you must specify in “AndroidManifest.xml” in “intent-filter” tag of “activity” tag.


Second we are setting up the class to call; in this case we are setting the class name for current activity but any other “class name” will also work provided that class can handle the Action specified in the intent.




Next we’ll create the container Intent that will have the Shortcut specific information.

// Then, set up the container intent (the response to the caller)

Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Name");
Parcelable iconResource = Intent.ShortcutIconResource.fromContext(
this, R.drawable.icon);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);

Here, in second line we are specifying the Intent that will be sent back to the activity when user clicks the shortcut Icon.

Next we specify the name i.e. the text that’ll be displayed with the Icon.


And then the Icon itself
.

Now, set the results for launcher to use
.

// Now, return the result to the launcher

setResult(RESULT_OK, intent);

Now write the following code in onCreate method of your Activity

// Resolve the intent
final Intent intent = getIntent();
final String action = intent.getAction();

// If the intent is a request to create a shortcut, we'll do that and exit

if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {
setupShortcut();
finish();
return;
}

Intent.ACTION_CREATE_SHORTCUT is sent to your activity by launcher when user has requested to create the Shortcut .
So, in response to this Intent we set up the intent and finish the activity and return.
With this your process of adding Activity shortcut to Launcher is finished.
Cheers!!
Vivek Jain
Hello Everyone, I came across an application which allowed me to produce nice effects on Images that are in my phone and Images that I take from my phones camera, its available here, 

1 comment:

  1. NIce and informative article, thanks for sharing this information.

    Great Effort, Keep Posting

    ReplyDelete