首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >给App的应用页面注册快捷方式

给App的应用页面注册快捷方式

作者头像
aqi00
发布2020-05-12 10:01:05
9290
发布2020-05-12 10:01:05
举报
文章被收录于专栏:老欧说安卓老欧说安卓

元数据不单单能传递简单的字符串参数,还能传送更复杂的资源数据,从Android7.1开始新增的快捷方式便用到了这点,譬如在手机上桌面长按支付宝图标,会弹出如下图所示的菜单。

点击菜单项“扫一扫”,直接打开支付宝的扫码页面;点击菜单项“付钱”,直接打开支付宝的付款页面;点击菜单项“收钱”,直接打开支付宝的收款页面。如此不必打开支付宝首页,即可迅速跳转到常用的App页面,这便是所谓的快捷方式。 那么Android7.1又是如何实现快捷方式的呢?那得再琢磨琢磨元数据了。原来元数据的meta-data标签除了前面说到的name属性和value属性,还拥有resource属性,该属性可指定一个XML文件,表示元数据想要的复杂信息保存于XML数据之中。借助元数据以及指定的XML配置,方可完成快捷方式功能,具体的实现过程介绍如下: 首先打开res/values目录下的strings.xml,在resources节点内部添加下述的三组(每组两个,共六个)字符串配置,每组都代表一个菜单项,每组又分为长名称和短名称,平时优先展示长名称,当长名称放不下时才展示短名称。这三组字符串的配置定义示例如下:

    <string name="first_short">first</string>
    <string name="first_long">扫一扫</string>
    <string name="second_short">second</string>
    <string name="second_long">付钱</string>
    <string name="third_short">third</string>
    <string name="third_long">收钱</string>

接着在res目录下创建名叫xml的文件夹,并在该文件夹创建shortcuts.xml,这个xml文件用来保存三组菜单项的快捷方式定义,文件内容如下所示:

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:shortcutId="first"
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:shortcutShortLabel="@string/first_short"
        android:shortcutLongLabel="@string/first_long">
        <!-- targetClass指定了点击该项菜单后要打开哪个活动页面 -->
        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.example.test"
            android:targetClass="com.example.test.ScanActivity" />
        <categories android:name="android.shortcut.conversation"/>
    </shortcut>

    <shortcut
        android:shortcutId="second"
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:shortcutShortLabel="@string/second_short"
        android:shortcutLongLabel="@string/second_long">
        <!-- targetClass指定了点击该项菜单后要打开哪个活动页面 -->
        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.example.test"
            android:targetClass="com.example.test.PayActivity" />
        <categories android:name="android.shortcut.conversation"/>
    </shortcut>

    <shortcut
        android:shortcutId="third"
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:shortcutShortLabel="@string/third_short"
        android:shortcutLongLabel="@string/third_long">
        <!-- targetClass指定了点击该项菜单后要打开哪个活动页面 -->
        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.example.test"
            android:targetClass="com.example.test.ReceiActivity" />
        <categories android:name="android.shortcut.conversation"/>
    </shortcut>
</shortcuts>

由上面的xml例子看到,每个shortcut节点都代表了一个菜单项,该节点的各属性说明如下: shortcutId:快捷方式的编号。 enabled:是否启用快捷方式。true表示启用,false表示禁用。 icon:快捷菜单左侧的图标。 shortcutShortLabel:快捷菜单的短标签。 shortcutLongLabel:快捷菜单的长标签。优先展示长标签的文本,长标签放不下时才展示短标签的文本。 以上的节点属性仅仅指明了每项菜单的基本规格,点击菜单项之后的跳转动作还要由shortcut内部的intent节点定义,该节点主要有targetPackage与targetClass两个属性需要修改,其中targetPackage属性固定为当前App的包名,而targetClass属性描述了菜单项跳转页面类的完整路径。 然后打开AndroidManifest.xml,找到MainActivity所在的activity节点,在该节点内部补充如下的元数据配置,其中name属性为android.app.shortcuts,而resource属性为@xml/shortcuts:

        <meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts" />

这行元数据的作用,是告诉App首页有个快捷方式菜单,其资源内容参见位于xml目录下的shortcuts.xml。完整的activity节点配置示例如下:

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <!-- 指定快捷方式。在桌面上长按应用图标,就会弹出@xml/shortcuts所描述的快捷菜单 -->
        <meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts" />
    </activity>

然后把测试应用安装到手机上,回到桌面长按应用图标,此时图标下方弹出下图所示的快捷菜单。

点击其中一个菜单项,果然跳到了配置的活动页面,证明元数据成功实现了类似支付宝的快捷方式。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-05-10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档