5Dec 2016

How to Use App Shortcuts in Android 7.1 Nougat

Comments   Android
how-to-use-app-shortcuts-in-android

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
             package="com.testapplication">
  <application ... >
    <activity android:name="Test">
      <intent-filter>
        <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/app_shortcuts" />
    </activity>
  </application>
</manifest>

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 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<shortcuts xmns:android="http://schemas.android.com/apk/res/android">
  <shortcut
    android:shortcutId="compose"
    android:enabled="true"
    android:icon="@drawable/compose_icon"
    android:shortcutShortLabel="@string/compose_shortcut_short_label1"
    android:shortcutLongLabel="@string/compose_shortcut_long_label1"
    android:shortcutDisabledMessage="@string/compose_disabled_message1">
    <intent
      android:action="android.intent.action.VIEW"
      android:targetPackage="com.example.myapplication"
      android:targetClass="com.example.myapplication.Compose
Activity" />
    <!-- If your shortcut is associated with multiple intents, include them
         here. The last intent in the list is what the user sees when they
         launch this shortcut. -->
    <categories android:name="android.shortcut.conversation" />
  </shortcut>
  <!-- Specify more shortcuts here. -->
</shortcuts>

2) Dynamic shortcuts :

Dynamic shortcuts are defined in class (.java)  files. So it can changes at run time in application.

To create dynamic shortcut,  ShortcutManager api is used. There are different type of 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 :

1
2
3
4
5
6
7
8
9
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("http://www.brevitysoftware.com/")))
    .build();
shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut));

Disabling Shortcuts

If you don’t want that users to select dynamic shortcuts. Than 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 appear when users attempt to launch a disabled dynamic shortcut , Use  disableShortcuts(List, CharSequence).

 

 

Brevity

About Admin

Brevity Software provides services such as : Web design and development, Mobile application development, Web application development, Travel portal development, Event management systems, Transport & Logistics Software, Enterprise Mobility Solutions, eCommerce & M Commerce application and many more.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>