分享

Android meta

 jnstyle 2018-02-02

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

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

  • <application>
  • <activity>
  • <activity-alias>
  • <provider>
  • <receiver>
  • <service>

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

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

实验代码

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas./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>
  • 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
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78

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);
    }
}
  • 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
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73

MyBroadcastReceiver.java

package com.example.metadatademo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context arg0, Intent arg1) {
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

MyContentProvider.java

package com.example.metadatademo;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;

public class MyContentProvider extends ContentProvider {

    @Override
    public int delete(Uri arg0, String arg1, String[] arg2) {
        return 0;
    }

    @Override
    public String getType(Uri arg0) {
        return null;
    }

    @Override
    public Uri insert(Uri arg0, ContentValues arg1) {
        return null;
    }

    @Override
    public boolean onCreate() {
        return false;
    }

    @Override
    public Cursor query(Uri arg0, String[] arg1, String arg2, String[] arg3, String arg4) {
        return null;
    }

    @Override
    public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
        return 0;
    }
}
  • 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
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

MyService.java

package com.example.metadatademo;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class MyService extends Service {

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

activity_main.xml

<RelativeLayout xmlns:android="http://schemas./apk/res/android"
    xmlns:tools="http://schemas./tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.metadatademo.MainActivity" >

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

运行截图

可以发现由于在AndroidManifest.xml中的<activity>标签的子标签<intent-filter>中存在以下两句,因此会生成2个应用图标,它们的名字是“android:label”的值。

<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
  • 1
  • 2

这里写图片描述

注意观察:虽然点击两个图标打开的都是同一个activity但是由于getComponentName()的返回值不同(分别代表了activity和activity-alias),因此,它们获取到的meta-data值也不同。

这里写图片描述

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多