Using Static Shortcuts
Static shortcuts should provide links to generic actions within your app, and these actions should remain consistent over the lifetime of your app's current version. Good candidates for static shortcuts include viewing sent messages, setting an alarm, and displaying a user's exercise activity for the day.
To create a static shortcut, complete the following sequence of steps:
In your app's manifest file (AndroidManifest.xml), find an activity whose intent filters are set to theandroid.intent.action.MAINaction and theandroid.intent.category.LAUNCHERcategory.
Add aelement to this activity that references the resource file where the app's shortcuts are defined:
package="com.example.myapplication">
android:resource="@xml/shortcuts"/>
Create a new resource file:res/xml/shortcuts.xml.
In this new resource file, add aroot element, which contains a list ofelements. Eachelement, in turn, contains information about a static shortcut, including its icon, its description labels, and the intents that it launches within the app:
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">
android:action="android.intent.action.VIEW"
android:targetPackage="com.example.myapplication"
android:targetClass="com.example.myapplication.ComposeActivity"/>
here. The last intent in the list determines what the user sees when
they launch this shortcut. -->
For more details about how to configure shortcuts in a resource file, see theShortcutManagerAPI reference.
Note:If you associate multiple intents with a shortcut, the system launches the activity corresponding to the shortcut's last intent in the resource file, with other activities in theback stack. In this case, when the user selects a shortcut then presses the back key, your app launches the activity corresponding with the shortcut's second-to-last intent listed in the resource file. This behavior pattern continues upon repeated presses of the back button, until the user clears the back stack that a shortcut created. When the user next presses the back button, the system navigates them back to the launcher.
Using Dynamic Shortcuts
Dynamic shortcuts should provide links to specific, context-sensitive actions within your app. These actions can change between uses of your app, and they can change even while your app is running. Good candidates for dynamic shortcuts include calling a specific person, navigating to a specific location, and viewing the current score for a specific game.
TheShortcutManagerAPI allows you to complete the following operations on dynamic shortcuts:
Publish:UsesetDynamicShortcuts()to redefine the entire list of dynamic shortcuts, or useaddDynamicShortcuts()to augment an existing list of dynamic shortcuts.
Update:Use theupdateShortcuts()method.
Remove:Remove a set of dynamic shortcuts usingremoveDynamicShortcuts(), or remove all dynamic shortcuts usingremoveAllDynamicShortcuts().
An example of creating a dynamic shortcut and associating it with your app appears in the following code snippet:
ShortcutManagershortcutManager=getSystemService(ShortcutManager.class);
ShortcutInfoshortcut=newShortcutInfo.Builder(this,"id1")
.setShortLabel("Web site")
.setLongLabel("Open the web site")
.setIcon(Icon.createWithResource(context,R.drawable.icon_website))
.setIntent(newIntent(Intent.ACTION_VIEW,
Uri.parse("https://www.mysite.example.com/")))
.build();
shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut));
网友评论