Shortcuts有两种实现方式
静态注册:在xml中定义, 适用于一些通用的动作.
动态注册: 由ShortcutManager发布, 可以根据用户的行为或者偏好添加, 可以动态更新.
1.静态使用,就是在xml中
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android"> <!--1.shortcutId, 不用多说, 这肯定是一个唯一的id--> <!--2.enabled, 表示这个shortcut是否可用--> <!--3.shortcutShortLabel, 这里是配置的短名称, 下面还会有长名称, 如果长名称显示不下, 就显示短名称--> <!--4.shortcutLongLabel, 这里是配置的长名称, launcher会优先选择长名称显示--> <!--5.shortcutDisabledMessage, 这个配置是在我们选择一个不可用的shortcut时给用户的一个--> <!--注意!注意!注意! 字符串必须引用,直接写会报错--> <!--注意!注意!注意! 静态使用shorts打开的activity,直接位于栈顶,会把其他的都抛出站,返回直接退出--> <shortcut android:shortcutId="settings" android:enabled="true" android:icon="@mipmap/ic_launcher" android:shortcutShortLabel="@string/short_name" android:shortcutLongLabel="@string/long_name" android:shortcutDisabledMessage="@string/cant_use_tag"> <!--1.intent, 这里表示我们点击shortcut时要干嘛, targetPackage是指定一个目标应用的包名, --> <!--2.targetClass是我们要跳转的目标类, 这里要注意的是android:action一定要配置, 否则会崩溃--> <!--3.categories, 这个东西目前位置官方只给提供了android.shortcut.conversation--> <intent android:action="android.intent.action.VIEW" android:targetPackage="com.yihui.com.androidv" android:targetClass="com.yihui.com.androidv.CreatFileActivity" /> <!--放两个,会先到后一个,点击返回,会回到上一个--> <intent android:action="android.intent.action.VIEW" android:targetPackage="com.yihui.com.androidv" android:targetClass="com.yihui.com.androidv.TextTypeActivity" /> <categories android:name="android.shortcut.conversation"/> </shortcut> <!--静态创建多个item--> <shortcut android:shortcutId="settings1" android:enabled="true" android:icon="@mipmap/ic_launcher" android:shortcutShortLabel="@string/short_name" android:shortcutLongLabel="@string/long_name" android:shortcutDisabledMessage="@string/cant_use_tag"> <intent android:action="android.intent.action.VIEW" android:targetPackage="com.yihui.com.androidv" android:targetClass="com.yihui.com.androidv.StateLayoutActivity" /> <categories android:name="android.shortcut.conversation"/> </shortcut> </shortcuts>
然后在要使用的的地方
2.动态注册
Shortcuts的总数不能超过5个, 即静态和动态shortcuts加起来总数最多是五个.
当我们尝试添加第六个shortcut时, 应用会抛出异常: java.lang.IllegalArgumentException: Max number of dynamic shortcuts exceeded
.