This is the plugin to create/delete Shortcuts (Android) in Apache Cordova/PhoneGap!
The Shortcut plugin for Apache Cordova allows you to create or delete the shortcut in the home of any andorid device.
<gap:plugin name="com.plugins.shortcut" />
or to use this exact version:
<gap:plugin name="com.plugins.shortcut" version="0.1.1" />
Android Install/Uninstall Shortcut Example
Nowadays many android apps installs a shortcut on home screen when you install the app and run it for the first time. This is a nice strategy to engage user by compiling them to use your app. Most of times when a user install an app, the app is deep buried in list of apps making it almost difficult to discover. So its always a good idea to make a shortcut right on home screen.
Android comes with simple yet undocumented API to add and remove shortcuts of any app on home screen. Let us check how to do this.
1. Install Shortcut on Home screen
Android provide us an intent class
com.android.launcher.action.INSTALL_SHORTCUT which can be used to add shortcuts to home screen. In following code snippet we create a shortcut of activity
MainActivitywith the name HelloWorldShortcut.
First we need to add permission INSTALL_SHORTCUT to android manifest xml.
The addShortcut() method create a new shortcut on Home screen.
Note how we create shortcutIntent object which holds our target activity. This intent object is added into another intent as EXTRA_SHORTCUT_INTENT. Finally we broadcast the new intent. This adds a shortcut with name mentioned as
EXTRA_SHORTCUT_NAME and icon defined by EXTRA_SHORTCUT_ICON_RESOURCE.
Note: One thing worth noting here is when you define your activity that is invoked from shortcut, you must define android:exported=”true” attribute in
<activity> tag. You’ll see this in our demo.
2. Uninstall Shortcut from Home screen
Similar to install, uninstalling or removing shortcut in Android uses an Intent (UNINSTALL_SHORTCUT) to perform the task. In following code we remove the shortcut added on home screen.
Again we need a permission to perform uninstall shortcut task. Add following permission to Android manifest xml.
removeShortcut() method does exactly reverse of addShortcut(). Most of the code is similar except removeShortcut calls
Let us check the full functionality by creating a demo application.
3. Demo Application
The demo application is very simple. The user interface has two buttons: one to add shortcut to home screen and another to remove it.
Following is the simple layout consiting of two buttons.
The layout looks something like following:
The MainAcitivity is the activity class that handles user interface events. We add OnClickListener event handlers to both Add and Remove shortcut buttons. Each calls appropriate method to perform the task.
Also following is the Android manifest file for reference.
Screenshots of App
On clicking Add Shortcut button a toast message appears saying the shortcut has been successfully added on home screen.
If you check the home screen, you will find the shortcut present.
The remove shortcut button triggers UNINSTALL_SHORTCUT intent and removes the shortcut from home screen. It also prints a toast message with confirmation.