How to use app shortcuts in Android 7.1 Nougat
From Android 7.1 nougat, You can define shortcuts to specific actions in your Android Application. App shortcut menu will be appear on long press on launcher icon of app. By using app shortcut user can quickly start common task in application.
Generally Android App Shortcuts used to quickly perform bellow tasks :
- Navigating to a specific location
- To sent messages
- To set alarm
- The score for a specific game
- Playing the next episode of a TV show in a media app.
You can publish two types of Android App Shortcuts as like below :
1) Static shortcuts
2) Dynamic shortcuts
1) Static shortcuts:
Static shortcuts are defined in resource (.xml) files. So it remain consistent over the lifetime of app’s current version.In this type of shortcut, you must wait until update of entire app to change the details of static shortcuts.
To create static shortcut follow bellow steps :
Step 1: In app’s manifest file (AndroidManifest.xml), set intent filters to android.intent.action.MAIN action and category to android.intent.category.LAUNCHER of any activity.
Step 2: Also set <meta-data> with android:name=”android.app.shortcuts” to same activity that references the resource file where the app’s shortcuts are defined.
Step 3: Create a new resource file (res/xml/app_shortcuts.xml) where the app’s manifest shortcuts are defined.
Step 4: In this .xml have <shortcuts> as root element.Bellow that root element you can set list of shortcut items by using <shortcut> elements.In <shortcut> elements, We can provide its icon, its description labels, and the intents that it launches within the app.
Example :
2) Dynamic shortcuts:
Dynamic shortcuts are defined in class (.java) files. So it can change at runtime in the application.
To create a dynamic shortcut, ShortcutManager API is used. There are different methods available as below:
i) Publish: To publish shortcut,Use setDynamicShortcuts(List) to define list of dynamic shortcuts or use addDynamicShortcuts(List) for existing list of dynamic shortcuts.
ii) Update: To update shortcut, Use updateShortcuts(List) method.
iii) Remove: To remove a set of dynamic shortcut,Use removeDynamicShortcuts(List), and to remove all dynamic shortcuts,Use removeAllDynamicShortcuts().
Example :
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "dynamicId")
.setShortLabel("My site")
.setLongLabel("Open My web site")
.setIcon(Icon.createWithResource(context, R.drawable.icon_website))
.setIntent(new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.brevitysoftware.com/")))
.build();
shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut));
Disabling Shortcuts
If you don’t want the users to select dynamic shortcuts. Then, use disableShortcuts(List), which removes the specified dynamic shortcuts and disables any pinned copies of these dynamic shortcuts.
If you want to provide an error message that appears when users attempt to launch a disabled dynamic shortcut, Use disableShortcuts (List, CharSequence).
Want to Scale
Your Business? Let’s Meet & Discuss!
CANADA
30 Eglinton Ave W Mississauga, Ontario L5R 3E7
INDIA
3rd floor Purusharth Plaza, Amin Marg, Rajkot, Gujarat. 360002
Get a Quote Now
Let's delve into a thorough understanding of your challenges and explore potential solutions together