How to create shortcut

Showing posts with label How to create shortcut. Show all posts
Showing posts with label How to create shortcut. Show all posts

Wednesday, September 3, 2014

Installing & Uninstalling Home Screen Shortcut.....


This Article shows to create installing & uninstalling home screen shortcut.

1- Create method for adding shortcut to home screen(addShortcut()).

private void addShortcut() {
Intent shortcut = new Intent(this, MainActivity.class);
shortcut.setAction(Intent.ACTION_MAIN);
Intent add = new Intent();
add.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcut);
add.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Your Activity Name");
add.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher));
add.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(add);
    }

2- Create method for removing shortcut from home screen(removeShortcut()).

private void removeShortcut() {
Intent shortcut = new Intent(this, MainActivity.class);
shortcut.setAction(Intent.ACTION_MAIN);
Intent add = new Intent();
add.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcut);
add.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Your Activity Name");
add.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
sendBroadcast(add);
    }

3- Add following permissions to Manifest.xml.

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