posts - 296,comments - 1,views - 2995

一.所花时间

1h

二.代码量

40行

三.博客量

1篇

四.了解到的知识点

给应用页面注册快捷方式

元数据不单单能传递简单的字符串参数,还能传送更复杂的资源数据,从Android 7.1开始新增的快捷方
式便用到了这点

首先打开res/values目录下的strings.xml,在resources节点内部添加下述的3组(每组两个,共6个)字
符串配置,每组都代表一个菜单项,每组又分为长名称和短名称,平时优先展示长名称,当长名称放不
下时才展示短名称。这3组6个字符串的配置定义示例如下:

<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文件用来保存3组
菜单项的快捷方式定义,

<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.chapter04"
            android:targetClass="com.example.chapter04.ActStartActivity" />
        <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.chapter04"
            android:targetClass="com.example.chapter04.JumpFirstActivity" />
        <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.chapter04"
 android:targetClass="com.example.chapter04.LoginInputActivity" />
 <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>

 

posted on   leapss  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
历史上的今天:
2023-04-23 每日打卡-11.2
2023-04-23 每日打卡-11.1
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示