App Shortcuts (android25,7.1)

一. 图1是长按之后会出来的shortcuts,图2是把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/shortcat"/>//添加静态shortcut的xml文件,注意这个xml文件要放到xml-v25的文件夹下才行。
</activity>

shortcat.xml文件

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:enabled="true"//可用
        android:icon="@drawable/ic_accessibility_red_400_24dp"//显示在shortcut左边的图片和将shortcut添加为快捷桌面后的图标
        android:shortcutDisabledMessage="@string/static_shortcut_disabled_message"//暂时不知道有啥用
        android:shortcutId="static"//表示静态
        android:shortcutLongLabel="@string/static_shortcut_long_label_1"//显示在shortcut图片右边的文字
        android:shortcutShortLabel="@string/static_shortcut_short_label_1">//将shortcut添加为快捷桌面的名字
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="ccav.xulingyun.com.myapplication.ShortCutTest1Activity"//点击shortcut后要跳转的activity
            android:targetPackage="ccav.xulingyun.com.myapplication" />//包名
    </shortcut>
    <!--如果shortcut节点里面包括两个和两个以上的intent,最后一个才是最终跳转进去的acticity,而前面的intent是作为回退栈处理的,即跳转到最后一个activity后,按返回键会一步步返回到前面intent定义的activity。
    如果只有一个intent,按返回键会直接返回到桌面。-->
    <shortcut
        android:enabled="true"
        android:icon="@drawable/ic_account_balance_red_400_24dp"
        android:shortcutDisabledMessage="@string/static_shortcut_disabled_message"
        android:shortcutId="static_2"
        android:shortcutLongLabel="@string/static_shortcut_long_label_2"
        android:shortcutShortLabel="@string/static_shortcut_short_label_2">
        <intent
            android:action="android.intent.action.MAIN"
            android:targetClass="ccav.xulingyun.com.myapplication.MainActivity"
            android:targetPackage="ccav.xulingyun.com.myapplication" />
        <intent
            android:action="com.ddmeng.hellonougat.action.STATIC_SHORTCUT_2"
            android:targetClass="ccav.xulingyun.com.myapplication.ShortCutTest2Activity"
            android:targetPackage="ccav.xulingyun.com.myapplication" />
    </shortcut>

    <shortcut
        android:enabled="false"
        android:icon="@drawable/ic_account_balance_red_400_24dp"
        android:shortcutDisabledMessage="@string/static_shortcut_disabled_message"
        android:shortcutId="static_2"
        android:shortcutLongLabel="@string/static_shortcut_long_label_2"
        android:shortcutShortLabel="@string/static_shortcut_short_label_2">
        <intent
            android:action="android.intent.action.MAIN"
            android:targetClass="ccav.xulingyun.com.myapplication.MainActivity"
            android:targetPackage="ccav.xulingyun.com.myapplication" />
        <intent
            android:action="com.ddmeng.hellonougat.action.STATIC_SHORTCUT_2"
            android:targetClass="ccav.xulingyun.com.myapplication.ShortCutTest2Activity"
            android:targetPackage="ccav.xulingyun.com.myapplication" />
    </shortcut>
</shortcuts>

三.动态添加shortcuts

ShortcutManager sm = getSystemService(ShortcutManager.class);//获取shortcut的管理类
        ShortcutInfo sci = new ShortcutInfo.Builder(this,"ssd")
                .setShortLabel("哈哈哈")
                .setLongLabel("嘿嘿嘿")
                .setIcon(Icon.createWithResource(this,R.drawable.ic_accessibility_red_400_24dp))
                .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.csdn.net/")))
                .build();//动态给shortcut添加属性,看名字和静态对应一下就知道具体的功能了。
        sm.setDynamicShortcuts(Arrays.asList(sci));//添加到shortcut管理类里。

        sm.updateShortcuts(Arrays.asList(sci));//更新指定的shortcut
        sm.removeAllDynamicShortcuts();//移除所有的动态添加的shortcut
        sm.removeDynamicShortcuts(Arrays.asList("ssd"));//移除指定shortcutid的shortcut。
       <!--也可以动态指定shortcut的回退栈,即setIntents()-->

四.Shortcuts的个数限制

静态和动态shortcuts加起来总数最多是四个(官方说五个,不知道为啥).
全部用静态的时候,添加大于等于4个只能显示4个。
全部用动态的最多也只能添加5个,但是只能显示出4个,大于5的时候会报下面的错误
注意:动态和静态一起使用的时候数量不能超过五个,如果超过五个也会报线面的错误。而只是用静态的多少多可以,但是最多只能显示四个。

java.lang.IllegalArgumentException: Max number of dynamic shortcuts exceeded.

五.Shortcuts的显示次序

ShortcutInfo sci = new ShortcutInfo.Builder(this,"ssd")
                .setShortLabel("哈哈哈")
                .setRank(0)//最高等级就是0,静态的全部是0,所以离app的图标最近,动态的则根据等级依次排在静态的外边,等级相同或者没有等级根据生成的顺序排
                .setLongLabel("嘿嘿嘿")
                .setIcon(Icon.createWithResource(this,R.drawable.ic_accessibility_red_400_24dp))
                .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.csdn.net/")))
                .build();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值