Shortcuts provide specific types of content to your users by helping them quickly access parts of your app.
How you serve content with shortcuts depends on your use case and whether the context of the shortcut is application-based or user-based. Although the context of a static shortcut does not change and the context of a dynamic shortcut changes constantly, the context in both cases is controlled by your application. In cases where a user chooses how they want their app to deliver content to them, such as with a pinned shortcut, the user defines the context. The following scenarios show some use cases for each type of shortcut:
- Static shortcuts are best for applications that link to content using a consistent structure throughout user interaction with the application. . Since most launchers can only display four shortcuts at a time, static shortcuts are useful for common activities. For example, if the user wants to see her calendar or email in a specific way, using a static shortcut ensures that her experience when performing a routine task is consistent.
- Dynamic shortcuts are used for actions in applications that are context sensitive. Context-sensitive shortcuts adapt to the actions users take in an application. For example, if you create a game that allows the user to start from their current level upon launch, you should update the shortcut frequently. Using a dynamic shortcut allows the shortcut to update each time the user deletes a level.
- Pinned shortcuts are used for specific user-driven actions. For example, a user might want to pin a specific website to the launcher. This is beneficial because it allows the user to perform a custom action, such as navigating to the website in one step, more quickly than using a default instance of a browser.
Create static shortcuts
Static shortcuts provide links to generic actions within your application, and these actions must remain constant for the life of the current version of your application. Good candidates for static shortcuts include viewing sent messages, setting an alarm, and viewing the user’s exercise activity throughout the day.
To create a static shortcut, complete the following sequence of steps:
-
In your application’s manifest file (AndroidManifest.xml), locate a shortcut activity whose intent to filter are configured for the action android.intent.action.MAIN and the category android.intent.category.LAUNCHER .
-
Add a
android:shortcutShortLabel
A concise phrase that describes the purpose of the shortcut. When possible, limit the length of a shortcut’s “short description” to 10 characters.
For more information, see setShortLabel().
Note: The value of this attribute must be a resource string, such as @string/shortcut_short_label.
android:shortcutLongLabel
An extended phrase that describes the purpose of the shortcut. If there is enough space, the launcher displays this value instead of android:shortcutShortLabel . When possible, limit the length of a shortcut’s “long description” to 25 characters.
For more information, see setLongLabel().
Note: The value of this attribute must be a resource string, such as @string/shortcut_long_label.
android:shortcutDisabledMessage
The message that appears in a supported launcher when the user attempts to launch a disabled shortcut. The message should explain to the user why the shortcut is now disabled. The value of this attribute has no effect if android:enabled is true.
Note: The value of this attribute must be a resource string, such as @string/shortcut_disabled_message.
android:enabled
Determines whether the user can interact with the shortcut from a supported launcher. The default value of android:enabled is true. If you set it to false, you must also set an android:shortcutDisabledMessage that explains why you have disabled the shortcut. If you don’t think you need to provide such a message, it’s easier to remove the shortcut from the XML file entirely.
android:icon
The bitmap or responsive icon the launcher uses when displaying the shortcut to the user. This value can be the path to an image or the resource file that contains the image. Use responsive icons whenever possible to improve performance and consistency.
Note: Shortcut icons cannot include tints.
Configure Inner Elements
The XML file that lists an application’s static shortcuts supports the following elements within eachIt allows you to send dynamic shortcuts that can be displayed on both Android surfaces (like the launcher) and Google surfaces (like the Assistant). Using this library allows your users to easily discover their shortcuts to quickly access specific content or play actions in your app. For example, a messaging app could send a dynamic shortcut for a contact “Alex” after a user sends that person a message. After pressing that dynamic shortcut, if the user asks the Assistant, “Hey Google, send a message to Alex in ExampleApp”, the Assistant could launch ExampleApp and automatically configure it to send a message to Alex.
Dynamic shortcuts shipped with this library are not subject to device-enforced shortcut limits, allowing your app to send a shortcut whenever a user completes an associated action on your app. application. By pressing frequent shortcuts in this way, Google can understand your user’s usage patterns and suggest contextually relevant shortcuts to them. For example, the Assistant could learn from shortcuts sent from your fitness tracking app that a user typically goes for a run every morning and proactively suggest a “start running” shortcut when the user picks up their phone in the morning. tomorrow.
The Google Shortcut Integration Library does not provide any addressable functionality itself. Adding this library to your app allows Google surfaces to ingest the shortcuts your app sends via ShortcutManagerCompat.
To use this library in your application, follow these steps:
-
Update your gradle.properties file to support AndroidX libraries:
-
In app/build.gradle, add dependencies for the Google Shortcut Integration Library and ShortcutManagerCompat:
-
With the library dependencies added to your Android project, your application can use the pushDynamicShortcut() method of ShortcutManagerCompat to send dynamic shortcuts that will be eligible to display on the launcher and participating Google surfaces.
Note: We recommend using pushDynamicShortcut to send dynamic shortcuts using the Google Shortcut Integration Library. Your app can use other methods to publish shortcuts, but these may fail if they reach the maximum shortcut limit.
Create sticky shortcuts
In Android 8.0 (API level 26) and higher, you can create sticky shortcuts. Unlike static and dynamic shortcuts, sticky shortcuts appear in supported launchers as separate icons. Figure 1 shows the distinction between these two types of shortcuts.
Note: When attempting to pin a shortcut in a supported launcher, the user receives a confirmation dialog asking for permission to pin the shortcut. If the user doesn’t allow the shortcut to be pinned, the launcher cancels the request.
To pin a shortcut to a supported launcher using your app, complete the following sequence of steps:
- Use isRequestPinShortcutSupported() to verify that the device’s default launcher supports pinning in-app shortcuts.
-
Create a ShortcutInfo object in one of two ways, depending on whether the shortcut already exists:
- If the shortcut already exists, create a ShortcutInfo object that contain only the ID of the existing shortcut. The system automatically finds and fixes all other information related to the shortcut.
- If you’re anchoring a new shortcut, create a ShortcutInfo object that contains an id, an intent, and a short tag for the new shortcut.
Note: Because the system automatically backs up and restores pinned shortcuts, the IDs of these shortcuts must contain stable, constant strings or server-side identifiers, rather than locally generated identifiers that might not make sense on other devices.
-
Try pinning the shortcut to the device launcher by calling requestPinShortcut(). During this process, you can pass a PendingIntent object, which notifies your application only when the shortcut is successfully pinned.
Note: If the user does not allow the shortcut to be pinned to the launcher, your app doesn’t receive a callback.
After a shortcut is pinned, your app can update its content using the updateShortcuts() method. For more information, read Updating Shortcuts.
The following code snippet demonstrates how to create a pinned shortcut:
Note: Instances of the ShortcutManager class they must be obtained using Context.getSystemService(Class) with the ShortcutManager.class argument or Context.getSystemService(String) with the Context.SHORTCUT_SERVICE argument.
Note: Also see the support library APIs, isRequestPinShortcutSupported() and requestPinShortcut(), which work on Android 7.1 (API level 25) and earlier. The support library uses the deprecated extra EXTRA_SHORTCUT_INTENT to attempt the pinning process.
Create a custom shortcut activity
You can also create a specialized activity that helps users create shortcuts, complete with custom options and a confirmation button. Figure 2 shows an example of this type of activity in the Gmail app.
In your application’s manifest file, add ACTION_CREATE_SHORTCUT to the activity
-