软件编程
位置:首页>> 软件编程>> Android编程>> Android使用元数据实现配置信息的传递方法详细介绍

Android使用元数据实现配置信息的传递方法详细介绍

作者:Shewyoo  发布时间:2023-11-02 15:25:48 

标签:Android,元数据,传递,配置信息

前序

格式

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

什么场景需要使用?

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

一、在代码中获取元数据

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

  • 调用getPackageManager方法获得当前应用的包管理器;

  • 调用包管理器的getActivityInfo方法获得当前活动的信息对象;

  • 活动信息对象的metaData是Bundle包裹类型,调用包裹对象的getString即可获得指定名称的参数值。

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

清单文件

<activity
           android:name=".MetaDataActivity"
           android:exported="true">
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />
               <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
           <meta-data android:name="weather" android:value="xxx"/>
       </activity>

xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical">
   <TextView
       android:id="@+id/tv_meta"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"/>
</LinearLayout>

java类

public class MetaDataActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_meta_data);
       TextView tv_meta = findViewById(R.id.tv_meta);
       //获取应用包管理器
       PackageManager pm = getPackageManager();
           try {
               //从应用包管理器中获取当前的活动信息
               ActivityInfo info = pm.getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);
               //获取活动附加的元数据信息
               Bundle bundle = info.metaData;
               String weather = bundle.getString("weather");
               tv_meta.setText(weather);
           } catch (PackageManager.NameNotFoundException e) {
               e.printStackTrace();
           }
   }
}

运行结果

Android使用元数据实现配置信息的传递方法详细介绍

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

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

Android使用元数据实现配置信息的传递方法详细介绍

利用元数据配置快捷菜单

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

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

  • 在res/values/strings.xml添加各个菜单项名称的字符串配置

  • 创建res/xml/shortcuts.xml,在该文件中填入各组菜单项的快捷方式定义。

  • 给activity节点注册元数据的快捷菜单配置。

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

清单文件AndroidManifest.xml

<activity
           android:name=".ActStartActivity"
           android:exported="true">
           <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>

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

<resources xmlns:android="http://schemas.android.com/apk/res/android">
   <shortcut
       android:shortcutId="first"
       android:enabled="true"
       android:icon="@mipmap/ic_launcher"
       android:shortcutLongLabel="@string/first_long"
       android:shortcutShortLabel="@string/first_short">
<!--文字太长则显示shotLabel ↑-->
<!--点击选项跳转到的页面 ↓-->
       <intent
           android:action="android.intent.action.VIEW"
           android:targetPackage="com.example.chapter2"
           android:targetClass="com.example.chapter2.ActStartActivity"/>
       <categories android:name="android.shortcut.conversation"/>
   </shortcut>
</resources>

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

Android使用元数据实现配置信息的传递方法详细介绍

来源:https://blog.csdn.net/Tir_zhang/article/details/126956205

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com