If your android application target device Android N (API 25) or higher we can create shortcuts for the activities on launcher icon without using any library. There are two ways to create app shortcuts in android application Static shortcuts, Dynamic shortcuts.
1. Static app shortcuts.
First, declare meta-data in your manifest inside the activity which action is (android.intent.action.MAIN) and category is (android.intent.category.LAUNCHER).
1 2 |
<meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts" /> |
then, create shortcuts.xml inside res/xml. Inside your shortcuts.xml you can define icon for your shortcut.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android"> <shortcut android:shortcutId="compose" android:enabled="true" android:icon="@drawable/home_shortcut" android:shortcutShortLabel="@string/home" android:shortcutLongLabel="@string/home" android:shortcutDisabledMessage="@string/home"> <intent android:action="android.intent.action.VIEW" android:targetPackage="webkul.opencart.mobikul" android:targetClass="webkul.opencart.mobikul.MainActivity" /> <categories android:name="android.shortcut.conversation" /> </shortcut> </shortcuts> |
In an intent node in your Shortcuts.xml targetPackage define your Application ID and targetClass your target Activity in which you want to redirect to the user.
2. Using Dynamic Shortcuts
These shortcuts are constructed and included in the list with shortcuts on the fly. They can be created based on some usage statistics and can change over time. There is no need to publish a new version of your app, in order to update your dynamic shortcuts.
Initialize shortcutManger and create an object of shortcutinfo. In shortcutinfo there are method like setShortLabel,setIcon and setIntent where we can define the label,icon and intent.
1 2 3 4 5 6 7 8 9 10 11 |
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class); ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "id1") .setShortLabel("Web site") .setLongLabel("Open the web site") .setIcon(Icon.createWithResource(context, R.drawable.icon_website)) .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.mysite.example.com/"))) .build(); shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut)); |
If there any query feel free to comment we would like to help you.