Creating shortcuts through Intent in Android

We can create shortcut for an application through intent. Its not so difficult. First create an intent with the package name of the application and add some flags to it. Then create another intent and add the first intent to it as extras and broadcast the intent. That is it.

The complete code is,



public class CreateShortcut extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent shortcutIntent = new Intent();
Log.i("CreateShortcut", "Creating first intent called shortcutIntent");
shortcutIntent.setClassName(getPackageName(), "CreateShortcut");
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent intent = new Intent();
Log.i("CreateShortcut", "Creating intent for broadcasting");
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "ShortcutName");
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, false);
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(intent);
}
}



Steps for launching the shortcut is,


-> Long press on the empty part of the home screen

-> Select "Shortcuts" from the dialog that pop-up




-> Select "Applications" from the dialog that pop-up





-> Select "CreateShortcut" from the dialog that pop-up





-> Now, we can see the shortcut for our application in home screen.

Comments

Popular posts from this blog

AlarmManager and Notification in Android

Swipe Action and ViewFlipper in Android

Open Gallery and get the selected image in appplication