• 【Android】-- 利用元数据传递配置信息(配置快捷菜单)


    格式

                <meta-data android:name="weather" android:value="xxx"/>
    

    什么场景需要使用?

    使用第三方SDK,需要在APP应用内使用别的APP的整合包,如使用微信登录、某某地图等。


    一、在代码中获取元数据

    在java代码中,获取元数据信息的步骤分为下列三步:

    • 调用getPackageManager方法获得当前应用的包管理器;
    • 调用包管理器的getActivityInfo方法获得当前活动的信息对象;
    • 活动信息对象的metaData是Bundle包裹类型,调用包裹对象的getString即可获得指定名称的参数值。

    例:从清单文件中获取元数据并显示到屏幕上

    清单文件

    1. <activity
    2. android:name=".MetaDataActivity"
    3. android:exported="true">
    4. <intent-filter>
    5. <action android:name="android.intent.action.MAIN" />
    6. <category android:name="android.intent.category.LAUNCHER" />
    7. </intent-filter>
    8. <meta-data android:name="weather" android:value="xxx"/>
    9. </activity>

    xml文件

    1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. android:layout_width="match_parent"
    3. android:layout_height="match_parent"
    4. android:orientation="vertical">
    5. <TextView
    6. android:id="@+id/tv_meta"
    7. android:layout_width="match_parent"
    8. android:layout_height="wrap_content"/>
    9. </LinearLayout>

    java类

    1. public class MetaDataActivity extends AppCompatActivity {
    2. @Override
    3. protected void onCreate(Bundle savedInstanceState) {
    4. super.onCreate(savedInstanceState);
    5. setContentView(R.layout.activity_meta_data);
    6. TextView tv_meta = findViewById(R.id.tv_meta);
    7. //获取应用包管理器
    8. PackageManager pm = getPackageManager();
    9. try {
    10. //从应用包管理器中获取当前的活动信息
    11. ActivityInfo info = pm.getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);
    12. //获取活动附加的元数据信息
    13. Bundle bundle = info.metaData;
    14. String weather = bundle.getString("weather");
    15. tv_meta.setText(weather);
    16. } catch (PackageManager.NameNotFoundException e) {
    17. e.printStackTrace();
    18. }
    19. }
    20. }

    运行结果

    二、给应用页面注册快捷方式

    元数据不仅能传递简单的字符串参数,还能传送更复杂的资源数据,如支付宝的快捷式菜单。

    利用元数据配置快捷菜单

    元数据的meta-data标签除了前面的name属性和value属性,还拥有resource属性,该属性可指定一个XML文件,表示元数据想要的复杂信息保存于XML数据之中。

    利用元数据配置快捷菜单的步骤如下:

    • 在res/values/strings.xml添加各个菜单项名称的字符串配置
    • 创建res/xml/shortcuts.xml,在该文件中填入各组菜单项的快捷方式定义。
    • 给activity节点注册元数据的快捷菜单配置。

     例:长按应用出现快捷菜单

    清单文件AndroidManifest.xml

    1. <activity
    2. android:name=".ActStartActivity"
    3. android:exported="true">
    4. <intent-filter>
    5. <action android:name="android.intent.action.MAIN" />
    6. <category android:name="android.intent.category.LAUNCHER" />
    7. </intent-filter>
    8. <meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts"/>
    9. </activity>

    新建shortcuts.xml文件用于配置快捷菜单

    1. <resources xmlns:android="http://schemas.android.com/apk/res/android">
    2. <shortcut
    3. android:shortcutId="first"
    4. android:enabled="true"
    5. android:icon="@mipmap/ic_launcher"
    6. android:shortcutLongLabel="@string/first_long"
    7. android:shortcutShortLabel="@string/first_short">
    8. <!--文字太长则显示shotLabel ↑-->
    9. <!--点击选项跳转到的页面 ↓-->
    10. <intent
    11. android:action="android.intent.action.VIEW"
    12. android:targetPackage="com.example.chapter2"
    13. android:targetClass="com.example.chapter2.ActStartActivity"/>
    14. <categories android:name="android.shortcut.conversation"/>
    15. </shortcut>
    16. </resources>

    运行结果:长按出现快捷菜单


  • 相关阅读:
    【QT】信号和槽机制
    day14学习总结
    C# 统计指定文件夹下的文件
    LCR 176.判断是否为平衡二叉树
    由一亿多条仇恨言论训练后,这个AI机器人成了恶毒的“键盘侠”
    C/C++字符串和数组基本操作demo
    数据库基础
    14.信号量的代码实现
    云计算实验2 Spark分布式内存计算框架配置及编程案例
    【推荐系统】美团外卖推荐场景的深度位置交互网络DPIN的突破与畅想
  • 原文地址:https://blog.csdn.net/Tir_zhang/article/details/126956205