Android 7.1 新特性之 Shortcuts运用

Android 7.1 允许 App 自定义 Shortcuts,类似 iOS 的 3D touch。通过在桌面长按 App 弹出 Shortcut 列表,点击某个 Shortcut 快速进入某项操作,同时 Shortcut 可以拖动到桌面进行固定,如下图系统谷歌浏览器


1. Shortcuts 作用及分类

Shortcuts 为 App 常用操作提供了快速访问的方式,如上面谷歌浏览器。

目前该功能只能在 Android 7.1 系统桌面进行使用,不过第三方桌面可以通过API接入这个功能

该特性类似于BroadcastReceiver ,它可以通过静态和动态两种方式进行注册和添加

2. 动态 Shortcuts(Dynamic Shortcuts)

动态 ShortcutsDynamic Shortcuts 通过 ShortcutManager API 进行操作。可以动态添加、修改、删除。

        ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
        ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "id1")
                .setShortLabel("csdn.net")
                .setLongLabel("Open My Blog")
                .setDisabledMessage("Disabled")
                .setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))
                .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://blog.csdn.net/zhuchuan1110")))
                .build();
        ShortcutInfo shortcut1 = new ShortcutInfo.Builder(this, "id2")
                .setShortLabel("csdn.net")
                .setLongLabel("Open My Blog")
                .setDisabledMessage("Disabled")
                .setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))
                .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://blog.csdn.net/zhuchuan1110")))
                .build();
 	ArrayList<ShortcutInfo> arrayList = new ArrayList<>();
        arrayList.add(shortcut);
        arrayList.add(shortcut1);
        shortcutManager.setDynamicShortcuts(arrayList);

通过ShortcutInfo.Builder新建 ShortcutInfo,再通过shortcutManager添加即可。其他:
(1) setDynamicShortcuts(List)可以替换并添加所有 shortcut 列表;
(2) addDynamicShortcuts(List)可以添加新的 shortcut 到列表,超过最大个数会报异常;
(3) updateShortcuts(List)可以更新一组 shortcuts;
(4) removeDynamicShortcuts(List)removeAllDynamicShortcuts() 可以删除部分或所有 shortcuts。

 

ShortcutInfo的属性与 xml 中定义字段含义一致,shortcutId shortcutShortLabel intent 是必须设置的字段,并且intent必须设置Action


3. 静态 Shortcuts(Static Shortcuts)

静态 ShortcutsStatic Shortcuts通过在 Manifest文件 中声明添加。缺点是不可以修改,只能通过应用升级来添加新的静态 Shortcuts。添加主要分为两步:

3.1 AndroidManifest.xml 的 Main Launcher 对应的 Activity 内添加 meta-data

meta-data name 为android.app.shortcuts,如下:

<application
    ……>
    <activity android:name=".main.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>
</application>
必须在 Main Launcher 对应的 Activity 内设置,其中 android:resource 指向定义了 shortcuts 的资源文件。

3.2 资源文件中定义具体的 shortcuts

res 目录下新建 xml 文件夹,并新建 shortcuts.xml 文件,内容如下:

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:enabled="true"
        android:icon="@drawable/search"
        android:shortcutId="search"
        android:shortcutDisabledMessage="@string/disabled"
        android:shortcutLongLabel="@string/menu_label"
        android:shortcutShortLabel="@string/launcher_label">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.example.k.myapplication.MainActivity"
            android:targetPackage="com.example.k.myapplication"/>
        <intent
            ……/>
    </shortcut>
    ……
</shortcuts>

shortcuts元素为根,可以包含多个shortcut元素,每个shortcut元素表示一个 shortcut。其中属性分别表示:
(1) shortcutId表示 shortcut 唯一标识符,相同的 shortcutId 会被覆盖。必须字段。
(2) shortcutShortLabel为将 shortcut 拖动到桌面时显示的名字,官方建议不超过 10 个字符,必须字段。
(3) shortcutLongLabel为 shortcut 列表中每个 shortcut 的名字,不宜过长,如果过长或未设置默认会显示 ShortLabel,官方建议不超过 25 个字符。可选字段。
(4) icon为 shortcut 的 icon,在列表展示和拖动到桌面时显示需要,可选字段。
(5) enabled表示 shortcut 是否可用,false 表示禁用。xml 中这个属性几乎没有被设置为 false 的实际场景。
(6) shortcutDisabledMessage为已固定在桌面的 shortcut 被 Disabled 后点击时的 Toast 提示内容。可选字段。
(7) intent为点击 shortcut 时响应的 intent,必须字段。
这里可以添加多个 intent,但点击时不会启动所有 intent,而是启动最后一个 intent,在这个 intent 回退时会启动它前面一个 intent,相当于自动将所有 intent 添加到了堆栈。

 

intent可设置属性包括:
android:actionandroid:dataandroid:mimeTypeandroid:targetClassandroid:targetPackage,其中android:action为必须属性。


注意事项:

1、 最多添加 4 个 Shortcuts 以保持在启动器中显示的样式最佳
目前虽然说是 5 个,但实际最多只能添加 4 个

2、限制 Label 长度
其中shortcutShortLabel建议不超过 10 个字符,shortcutLongLabel 建议不超过 25 个字符

3、shortcutId 是唯一标识,相同 shortcutId 会被覆盖。

4、intent 必须设置 android:action 属性,同时目标 Activity 必须有效即已在 Manifest 中声明。






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值