Android 中 meta-data 用在 AndroidManifest.xml 文件中。<meta-data>标签是提供组件额外的数据用的,它本身就是一个键值对(Bundle),可以自定义名称和值(value或resource)。
<?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>
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); }}
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) { }}
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; }}
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; }}
<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>
可以发现由于在AndroidManifest.xml中的<activity>标签的子标签<intent-filter>中存在以下两句,因此会生成2个应用图标,它们的名字是“android:label”的值。
<action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" />
注意观察:虽然点击两个图标打开的都是同一个activity但是由于getComponentName()的返回值不同(分别代表了activity和activity-alias),因此,它们获取到的meta-data值也不同。
联系客服