meta-data 元数据初探

Android 中 meta-data 用在 AndroidManifest.xml 文件中。标签是提供组件额外的数据用的,它本身就是一个键值对(Bundle),可以自定义名称和值(value或resource)。

它可以包含在以下6个组件中:

meta-data 主要有以下几个常见用途:

  • 在APP发布的时候,通过修改 meta-data,来标记不同的“发布渠道”,以方便脚本自动化修改、打包、发布。
  • 与 activity-alias 配合使用,meta-data作为标记,来实现类似“拨号-联系人-短信”应用的通过同一个activity打开不同的选项卡。
  • 作为日志标记,在公共父类中获取该值并作为是否打印日志的标记,用多态化的思想控制日志,免去了冗杂的在代码中进行分别配置。

实验代码

Manifest.xml


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.metadatademo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="23"
        android:targetSdkVersion="25" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="application" >
        <activity
            android:name="MainActivity"
            android:label="activity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            intent-filter>

            <meta-data
                android:name="meta_data"
                android:value="activity_meta_data_value" />
        activity>

        <activity-alias
            android:name="MyActivityAlias"
            android:label="activity_alias"
            android:targetActivity=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.LAUNCHER" />
            intent-filter>

            <meta-data
                android:name="meta_data"
                android:value="activity_alias_meta_data_value" />
        activity-alias>

        <meta-data
            android:name="meta_data"
            android:value="application_meta_data_value" />

        <provider
            android:name="MyContentProvider"
            android:authorities="com.chy.authorities"
            android:label="provider" >
            <meta-data
                android:name="meta_data"
                android:value="provider_meta_data_value" />
        provider>

        <receiver
            android:name="MyBroadcastReceiver"
            android:label="receiver" >
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
            intent-filter>

            <meta-data
                android:name="meta_data"
                android:value="receiver_meta_data_value" />
        receiver>

        <service
            android:name="MyService"
            android:label="service" >
            <meta-data
                android:name="meta_data"
                android:value="service_meta_data_value" />
        service>
    application>
manifest>

MainActivity.java

package com.example.metadatademo;

import android.app.Activity;
import android.content.ComponentName;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ProviderInfo;
import android.content.pm.ServiceInfo;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

    private TextView textview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textview = (TextView) findViewById(R.id.textview);

        PackageManager pm = getPackageManager();

        //application meta-data
        try {
            ApplicationInfo applicationInfo = pm.getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
            addText(applicationInfo.metaData.getString("meta_data"));
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }

        //"activity""activity-alias" meta-data
        try {
            ActivityInfo activityInfo = pm.getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);
            addText(activityInfo.metaData.getString("meta_data"));
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }

        //"provider" meta-data
        ComponentName providerComponentInfo = new ComponentName(this, MyContentProvider.class);
        try {
            ProviderInfo providerInfo = pm.getProviderInfo(providerComponentInfo, PackageManager.GET_META_DATA);
            addText(providerInfo.metaData.getString("meta_data"));
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }

        //"receiver" meta-data
        ComponentName receiverComponentInfo = new ComponentName(this, MyBroadcastReceiver.class);
        try {
            ActivityInfo receiverInfo = pm.getReceiverInfo(receiverComponentInfo, PackageManager.GET_META_DATA);
            addText(receiverInfo.metaData.getString("meta_data"));
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }

        //"service" meta-data
        ComponentName serviceComponentInfo = new ComponentName(this, MyService.class);
        try {
            ServiceInfo serviceInfo = pm.getServiceInfo(serviceComponentInfo, PackageManager.GET_META_DATA);
            addText(serviceInfo.metaData.getString("meta_data"));
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    private void addText(String str) {
        textview.setText(textview.getText().toString() + "\n" + str);
    }
}
总结,取值方式与元数据放置位置相关。

你可能感兴趣的:(Android,元数据)