• Android开发学习日记--利用元数据给应用设置快捷方式(支付宝为例)


    一、效果展示

     二、实现步骤

        元数据不单单能传递简单的字符串参数,还能传送更复杂的资源数据,从 Android 7.1 开始新增的快捷方式便用到了这点。
    1. 首先打开 res/values 目录下的 strings.xml ,在 resources 节点内部添加下述的 3 组(每组两个,共 6 个)字 符串配置,每组都代表一个菜单项,每组又分为长名称和短名称,平时优先展示长名称,当长名称放不 下时才展示短名称。这3 6 个字符串的配置定义示例如下:
    1. 这里为了省事,只定义了三个字符串
    2. <resources>
    3. <string name="app_name">支付宝</string>
    4. <string name="sao">扫一扫</string>
    5. <string name="shou">收钱</string>
    6. <string name="fu">付钱</string>
    7. <string name="title_activity_main2">Main2Activity</string>
    8. </resources>

    2. 接着在res目录下创建名为xml的文件夹,并在该文件夹创建shortcuts.xml,这个XML文件用来保存3组 菜单项的快捷方式定义,文件内容如下所示:(注意,文件夹名和文件名一字不能错!)

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    3. <shortcut
    4. android:shortcutId="first" //设置id,是快捷方式的编号
    5. android:enabled="true" //是否启用快捷方式,如果为false则不会显示
    6. android:icon="@drawable/sao" //图标的位置
    7. android:shortcutShortLabel="@string/sao" //快捷菜单短标签
    8. android:shortcutLongLabel="@string/sao"> //快捷菜单长标签
    9. <intent //Intent必须要设置
    10. android:action="android.intent.action.VIEW" //基础操作
    11. android:targetPackage="com.example.helloworld" //目标包名
    12. android:targetClass="com.example.helloworld.MainActivity"/>
    13. //targetclass指定点击该项菜单后打开哪个活动页面
    14. <categories android:name="android.shortcut.conversation" /> //官方提供的
    15. </shortcut>
    16. <shortcut
    17. android:shortcutId="second"
    18. android:enabled="true"
    19. android:icon="@drawable/fu"
    20. android:shortcutShortLabel="@string/shou"
    21. android:shortcutLongLabel="@string/shou">
    22. <intent
    23. android:action="android.intent.action.VIEW"
    24. android:targetPackage="com.example.helloworld"
    25. android:targetClass="com.example.helloworld.MainActivity"/>
    26. <categories android:name="android.shortcut.conversation" />
    27. </shortcut>
    28. <shortcut
    29. android:shortcutId="third"
    30. android:enabled="true"
    31. android:icon="@drawable/fu"
    32. android:shortcutShortLabel="@string/fu"
    33. android:shortcutLongLabel="@string/fu">
    34. <intent
    35. android:action="android.intent.action.VIEW"
    36. android:targetPackage="com.example.helloworld"
    37. android:targetClass="com.example.helloworld.MainActivity"/>
    38. <categories android:name="android.shortcut.conversation" />
    39. </shortcut>
    40. </shortcuts>

    3. 然后打开AndroidManifest.xml,找到MainActivity所在的activity节点,在该节点内部补充如下的元数据配置,其中name属性为android.app.shortcuts,而resource属性为@xml/shortcuts。这行元数据的作用,是告诉App首页有个快捷方式菜单,其资源内容参见位于xml目录下shortcuts.xml。完整的activity节点配置示例如下:

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    3. package="com.example.helloworld">
    4. <application
    5. android:allowBackup="true"
    6. android:icon="@mipmap/ic_launcher"
    7. android:label="@string/app_name"
    8. android:roundIcon="@mipmap/ic_launcher_round"
    9. android:supportsRtl="true">
    10. <activity
    11. android:name=".Main2Activity"
    12. android:theme="@style/AppTheme.NoActionBar" />
    13. <activity
    14. android:name=".MainActivity"
    15. android:launchMode="standard"
    16. android:theme="@style/Theme.AppCompat.Light"
    17. android:exported="true">
    18. <intent-filter>
    19. <action android:name="android.intent.action.VIEW" />
    20. <action android:name="android.intent.action.MAIN" />
    21. <category android:name="android.intent.category.LAUNCHER" />
    22. </intent-filter>
    23. //写在这里就行
    24. <meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts"/>
    25. </activity>
    26. </application>
    27. </manifest>

  • 相关阅读:
    孩子自律性不够?猿辅导:计划表要注意“留白”给孩子更多掌控感
    如何运行黑马程序员redis项目黑马点评(hm-dianping)、常见报错解决与部分接口的测试方法
    华为OD机考算法题:简单的自动曝光
    在linux下使用ffmpeg方法
    php案例:今天是星期几呢?
    总结40条常用Linux命令的基本使用
    【Vue五分钟】五分钟了解webpack实战配置案例详情
    推荐一本书《变速领导力》
    jenkins 共用宿主机中的docker自动化部署
    AMD AFMF不但能用在游戏,也适用于视频
  • 原文地址:https://blog.csdn.net/m0_52238102/article/details/125538871