[Android Software Development] Geeks Empire:
Implement AppShortcuts [Static|Dynamic]

[IMG="for fun"]https://lh3.googleusercontent.com/5RM5FsviY5rVG2r5NgU1fnvDe5LS0Nd-X8_KBcKZ6GbFxso80uAvEsYBuRxOJzpKlbjMHu8T=w576-h324-p-rw[/IMG]​

# Static Shortcuts

(If you have Camera App, it is reasonable to create static shortcuts to Selfie Activity, Video Recording or etc.)

Like other resources, you need to define Static Shortcuts inside the .xml file under app/res/xml/static_shortcuts_info.xml and call it inside AndroidManifest.xml

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:shortcutId="some_string_must_be_different_for_each"
android:enabled="true"
android:icon="@drawable/icon"
android:shortcutShortLabel="text"
android:shortcutLongLabel="text"
android:shortcutDisabledMessage="warning_text">
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="com.test.application"
android:targetClass="com.test.application.SomeActivity" />
<!-- If your shortcut is associated with multiple intents, include them here. The last intent in the list determines what the user sees whenthey launch this shortcut. -->
<categories android:name="android.shortcut.conversation" />
</shortcut>
<!-- Add More* -->
</shortcuts>

* Add more AppShortcut is limited to each activity with ACTION_MAIN & LAUNCHER Category in intent filter

<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

Get limited amount of AppShortcuts (Static/Daynamic combined) with

getSystemService(ShortcutManager.class).getMaxShortcutCountPerActivity()

after creating Static Shortcuts Info call them inside Manifest as meta-data inside activity tag with mentioned intent filter.

<activity android:name="Main">
<intent-filter>
<!-- activity with this intent filter can have app shortcut -->
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="android.app.shortcuts" android:resource="@xml/static_shortcuts_info.xml" />
</activity>

Done!

# Dynamic Shortcuts

(If you have Online Shopping App, it is reasonable to implement Dynamic Shortcuts to New Available Product, Special Deals Information to show names or prices)

You need to add dynamic shortcuts in runtime. for example @onStart(Bundle){}

ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
List<ShortcutInfo> shortcutInfos = new ArrayList<ShortcutInfo>();

ShortcutInfo firstShortcut = new ShortcutInfo.Builder(getApplicationContext(), "different_text_for_each")
.setShortLabel("Deals")
.setLongLabel("Smartphone 20% Off")
.setIcon(Icon.createWithResource(context, R.drawable.icon_website))
.setIntent(new Intent(context, Activity.class))
.build();

ShortcutInfo secondShortcut = new ShortcutInfo.Builder(getApplicationContext(), "different_text_for_each")
.setShortLabel("NEW Phone")
.setLongLabel("Available April 1st")
.setIcon(Icon.createWithResource(context, R.drawable.icon_website))
.setIntent(new Intent(context, Activity.class))
.build();

shortcutInfos.add(firstShortcut);
shortcutInfos.add(secondShortcut);

shortcutManager.setDynamicShortcuts(shortcutInfos);
//after first time you can just update the value by [B]updateShortcuts(List<ShortcutInfo>)[/B]

You can remove one shortcut by calling removeDynamicShortcuts and set it is specific id (string)

removeDynamicShortcuts("string_of_id")

Or Just call this method

removeAllDynamicShortcuts()

Done!