Android App Shortcuts使用

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值