Saturday, March 18, 2017

Android - App Shortcuts, Android Nougat 7.1 Feature.

Android Nougat 7.1, the newest version of Android has come with several new features. Here I'm going to explain App Shortcuts. App Shortcuts allows the user to move on a specific screen from the app launcher. The feature is available on any launcher that supports them, such as YouTube app launchers, Android Nougat 7.1.


App Shortcuts, YouTube in Android Nougat 7.1


To reveal the shortcuts of an app, simply long-press the launcher icon of that app. Then tap on a shortcut to jump to the associated action. These shortcuts are a great way to engage users and provide some menu options of your app even before users launch your app.

Each shortcut references an intent, each of which launches a specific action or task, and you can create a shortcut for any action that you can express as an intent. For example, you can create intents for sending a new text message, making a reservation, playing a video, continuing a game, loading a map location, and much more.

Implement it we have two ways

 A) Statically  (by declaring all the shortcuts in a resource file, also known as manifest shortcuts).

 B) Dynamically (by adding the shortcuts at runtime).

Let's talk about the static approach.

1. Create a shortcuts.xml file and keep it on following location in the project.

 res/xml/shortcuts.xml
 res/xml-v25/shortcuts.xml
 <shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
            <shortcut
               android:enabled="true"
               android:icon="@drawable/ic_launcher"
               android:shortcutDisabledMessage="@string/shortcut_label_disabled"
               android:shortcutId="new_task"
               android:shortcutLongLabel="@string/shortcut_label_create_new_task"
               android:shortcutShortLabel="@string/shortcut_label_new_task">
                  <intent
                     android:action="android.intent.action.VIEW"
                     android:targetClass="com.test.launchershortcut.DetailActivity"
                     android:targetPackage="com.test.launchershortcut"/>
            </shortcut>

            <shortcut
               android:enabled="true"
               android:icon="@drawable/ic_launcher
               android:shortcutDisabledMessage="@string/shortcut_label_disabled"
               android:shortcutId="opened_tasks"
               android:shortcutLongLabel="@string/shortcut_label_view_opened_tasks"
               android:shortcutShortLabel="@string/shortcut_label_opened_tasks">
                  <intent
                    android:action="android.intent.action.VIEW"
                    android:targetClass="com.test.launchershortcut.ReportActivity
                    android:targetPackage="com.test.launchershortcut"/>
             </shortcut>

  </shortcuts>  

As we can see each shortcut tag defines a series of attributes, like the icon and labels, and also references an intent which is set to launch a specific activity when triggered. Attributes android:shortcutId is a mandatory attribute. If it is not declared it will cause the respective shortcut not to appear in the shortcuts list. The recommended maximum number of shortcuts is 4, although it is possible to publish up to 5.


2. Reference shortcuts.xml file in the AndroidManifest.xml as metadata to the App’s launcher activity.
<activity android:name=".MainActivity">
     <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/shortcuts" />
</activity>

Any activity that has the intent-filter action set to android.intent.action.MAIN and the category to android.intent.category.LAUNCHER can display app shortcuts.

Now run the project, the result might look something like this



Implementation of Dynamically Approach. 

For handle Static and Dynamic App shortcuts Nougat 7.1 provides us ShortcutManager. ShortcutManager is the entry point for manipulating (adding, updating, removing) shortcuts at runtime. 
private void createDynamicAppShortCut() {
       ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
        Intent intent = new Intent(this, DetailActivity.class);
        intent.setAction(Intent.ACTION_VIEW);
        ShortcutInfo shortcut = new ShortcutInfo.Builder(this,                    getString(R.string.shortcut_label_new_task_dy))
                .setShortLabel(getString(R.string.shortcut_label_new_task_dy))
                .setLongLabel(getString(R.string.shortcut_label_new_task_dy))
                .setIcon(Icon.createWithResource(this, R.drawable.ic_launcher))
                .setIntent(intent)
                .build();
        shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut));
}

Trigger the above method at a runtime and move to App Launcher, You will see, we have one more shortcut.



ShortcutManager provides us with some interesting methods.

updateShortcuts (List ids) – update all existing shortcuts by ID.

removeDynamicShortcuts (List ids) – delete dynamic shortcuts by ID.

disableShortcuts (List ids) – disable dynamic shortcuts by ID.

reportShortcutUsed (String shortcutId) – You want to call this method whenever the user selects the shortcut containing the given ID or when the user completes an action in the application that is equivalent to selecting the shortcut. 

Something more

If you tap and drag a shortcut then it will be pinned to the device’s launcher:



When App Shortcuts disable or updated as used, Icon color will be changed. Something like this





Share:

0 comments:

Post a Comment

We're Social

Categories

Ad here

  • Cover art Blend

    Blend app is very advanced app there are lots of features like shayari, status, chat, feed....