打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Android meta

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.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>
  • 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.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/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值也不同。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Using the Android action bar (ActionBar)
两个android程序间的相互调用(apk互调)
Android中自定义权限
Android支付接入(二):移动游戏基地
Android基础之四大组件
Android 利用scheme页面内跳转协议进行跳转
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服