Hello shortcuts

Starting with Android7.1 (API level25), developers can customize shortcuts for their apps. Shortcuts will make it easier and faster to use your app. I personally feel a bit like ios pressure sensing, but I think Google’s shortcuts animation does a better job :).

There are two kinds of Shortcuts:

  1. Static Shortcuts: Static Shortcuts are defined in the resource file, so you can only update information about Static Shortcuts by upgrading your app.

  2. Dynamic shortcuts: Dynamic shortcuts are created to add, modify, and remove shortcuts at runtime using the API associated with ShortcutManager.

Other tips for Shortcuts:

  1. You can set up a maximum of five shortcuts (although you can only display a maximum of four in the test). Some launcher apps don’t display all the shortcuts you’ve added.

  2. Users can hold shortcuts to their desktop, which Google calls “pinned Shortcuts” and there is no limit to the number of shortcuts, And developers do not have the right to remove these shorcuts(they can only be removed automatically if the user removes or deletes the app, if a Certain Shortcuts are pinned to the desktop, even if the shortcuts are dynamically deleted, the desktop Shortcuts will not disappear and work as expected), But you can make it disbale.

  3. While other apps don’t have access to your app’s metadata with Shortcuts, Laucher does, so be careful to protect your users’ private information when using Shortcuts.

Use of Static Shortcuts

  1. Add meta-data to the startup page in the Manifest file
 <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>
Copy the code
  1. Create a new folder XML in the res folder and create a new file shortcuts. XML

    //shortcuts.xml
    
    <shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:shortcutId="shortCutId1"
        android:enabled="true"
        android:icon="@drawable/icon_android"
        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.zengyazhi.myapplication"
            android:targetClass="com.example.zengyazhi.myapplication.Main1Activity" />
        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.example.zengyazhi.myapplication"
            android:targetClass="com.example.zengyazhi.myapplication.Main2Activity" />
        <categories android:name="android.shortcut.conversation" />
    </shortcut>
    
    <shortcut
        android:shortcutId="shortCutId2"
        android:enabled="true"
        android:icon="@drawable/icon_google"
        android:shortcutShortLabel="@string/compose_shortcut_short_label2"
        android:shortcutLongLabel="@string/compose_shortcut_long_label2"
        android:shortcutDisabledMessage="@string/compose_disabled_message2">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.example.zengyazhi.myapplication"
            android:targetClass="com.example.zengyazhi.myapplication.Main2Activity" />
        <categories android:name="android.shortcut.conversation" />
    </shortcut>
    Copy the code

    Explain the attributes of the shortcut TAB:

    • ShortcutId: Shortcuts ID
    • Enabled: If it is false it will not show up in the Shortcuts list
    • Icon: Shortcuts icon
    • ShortcutShortLabel: Title for when Shortcuts are fixed to the desktop (note that strings can only be referenced using string resource files, not strings directly)
    • ShortcutLongLabel: The title for shortcuts when you hold down the app. If it’s too long or not set, the shortcutShortLabel will be displayed by default
    • ShortcutDisabledMessage: Toast message when pinned Shortcuts are unavailable
    • There can be multiple tabs within the Shortcuts TAB, such as page 1, page 2, and page 3, and the user clicking on Shortcuts will take them to the last TAB in the list (page 3) and will be able to navigate back to page 2 and then page 1.

Use of Dynamic Shortcuts

Use the ShortcutManager API to create, update, and remove shortcuts. The apis are used similarly.

  1. usesetDynamicShortcuts()andaddDynamicShortcuts()Add shortcuts dynamically. These two methods are somewhat similar, so be careful when using them.addDynamicShortcuts(): Add Shortcuts, update information if there is a Shortcuts with the same ID.setDynamicShortcuts():replaceExisting dynamic Shortcuts list, updated if there are shortcuts with the same ID. For example: For example there was originally a Shortcuts list:
  • Tag 1 (Lable: Zhang SAN, ID: ONE)

  • Tag 2 (lable: Li Si, ID: two)

  • Tag three (lable: King five, ID: three)

    Use the setDynamicShortcuts() method and pass in two ShortcutInfo:

  • Tag 3 (Lable: Zhao Liu, ID: Three)

  • Tag 4 (lable: seven, ID: four)

    Calling the method causes the Shortcuts list to change to:

  • Tag 3 (Lable: Zhao Liu, ID: Three)

  • Tag 4 (lable: seven, ID: four)

    Because tag 3 has the same ID, tag 3 is updated, and tags 1 and 2 from the Shortcuts list have been removed, also mentioned at the beginning of the introduction to Shortcuts: If Shortcuts are pinned to the desktop become shortcuts, even if shortcuts are removed from the list, the desktop shortcuts will still work unless you set them to disable, that is, if You have pins pinned to the desktop, Even if a long press on the app after calling setDynamicShortcuts() doesn’t show s3s and S3s, the s3S on the desktop won’t disappear and will work fine.

  1. Update information for Shortcuts with updateShortcuts()

    ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
    ShortcutInfo shortCutId1 = new ShortcutInfo.Builder(MainActivity.this, "shortCutId3")
            .setShortLabel("Change desktop Label 3")
            .setLongLabel("Change shortcut TAB 3")
            .setIcon(Icon.createWithResource(this, R.drawable.icon_chrome))
            .setDisabledMessage("Change unavailable prompt 3")
            .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com/"), this, Main1Activity.class))
            .build();
    shortcutManager.updateShortcuts(Arrays.asList(shortCutId1, shortCutId4));
    Copy the code

    Dynamic Shortcuts can also add multiple intent intents at the same time just like static Shortcuts

    Intent intent1 = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com/"), this, Main1Activity.class);
    Intent intent2 = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com/"), this, Main2Activity.class); Intent[] intents = {intent1, intent2}; . .setIntents(intents) .build(); .Copy the code
  2. Use the removeDynamicShortcuts () to remove a single or multiple dynamic shortcuts, using removeAllDynamicShortcuts () removes all dynamic shortcuts

    shortcutManager.removeDynamicShortcuts(Arrays.asList("shortCutId31"."shortCutId4"));
    
    shortcutManager.removeAllDynamicShortcuts();
    Copy the code
  3. Set Shortcuts to unavailable:

    shortcutManager.disableShortcuts(Arrays.asList("shortCutId3")); / / another overloaded methods, can display an error message when the user clicks on the shortcuts shortcutManager. DisableShortcuts (arrays.aslist ("shortCutId4"), "Shortcut 4 is no longer available");
    Copy the code

Keep in mind that the above API can only operate on dynamic Shortcuts (including pinned Shortcuts), but cannot operate on static Shortcuts, if you pass a static SHORTCUTS ID you get an IllegalArgumentException: Manifest shortcut ID=*** may not be manipulated via APIs

Track the use of Shortcuts

ReportShortcutUsed () is called for the following scenarios:

  1. The user hits Shortcuts

  2. The user takes the action associated with Shortcuts

Report the use of Shortcuts to anticipate the priorities of Shortcuts and help developers use Them better.

Shortcuts Design specification

App Shortcuts Design Guidelines

Android from the material design design specification launched to today’s Android7, I think it is really comparable to iOS, but it has not been widely respected, it is really a sigh. In addition, the Nexus5 released in 2013 feels smoother than before after upgrading to Android7. What is conscience? Shout it out with me: Yes, Google!