概述
效果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/56e637bc23f27657eb1f3dbcfa029346.jpeg)
好处
- 可以方便用户直接在桌面跳到目标页面
- 可以让你的App显得更加专业(不过切忌添加过多,1-3个就够了)
添加方式
静态添加
- 在资源文件中添加 xml 目录,并在里面创建 shortcuts.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:enabled="true"
android:icon="@mipmap/icon1"
android:shortcutDisabledMessage="@string/static_message"
android:shortcutId="static1"
android:shortcutLongLabel="@string/static_long_label_1"
android:shortcutShortLabel="@string/static_short_label_1">
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.test.shortcutsdemo.StaticTestActivity1"
android:targetPackage="com.test.shortcutsdemo" />
</shortcut>
<shortcut
android:enabled="true"
android:icon="@mipmap/icon2"
android:shortcutDisabledMessage="@string/static_message"
android:shortcutId="static2"
android:shortcutLongLabel="@string/static_long_label_2"
android:shortcutShortLabel="@string/static_short_label_2">
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.test.shortcutsdemo.StaticTestActivity2"
android:targetPackage="com.test.shortcutsdemo" />
</shortcut>
</shortcuts>
<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>
动态添加
val shortcutList = mutableListOf<ShortcutInfo>()
val shortcut1 = ShortcutInfo.Builder(this, "Dynamic1").apply {
setShortLabel("动态快捷1")
setLongLabel("D1")
setIcon(Icon.createWithResource(this@MainActivity, R.mipmap.icon3))
setIntent(Intent().apply {
action = Intent.ACTION_MAIN
setClass(this@MainActivity, DynamicTestActivity::class.java)
putExtra("info", "Dynamic shortcuts target class with intent1")
})
}.build()
shortcutList.add(shortcutList);
shortcutManager.dynamicShortcuts = shortcutList