應(yīng)用快捷方式

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));




最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容