Meta data in Android is used in the Android manifest.xml file. The < meta data > tag is used to provide additional data for components. It is a key value pair in itself. You can customize the name and value or resource.
It can be included in the following six components:
- <application>
- <activity>
- <activity-alias>
- <provider>
- <receiver>
- <service>
Meta data is mainly used for the following common purposes:
- When APP is released, different "release channels" are marked by modifying meta data to facilitate script automatic modification, packaging and release.
- It is used with activity alias and meta data is used as a mark to realize the similar "dial contact SMS" application by opening different tabs with the same activity.
- As a log mark, this value is obtained in the public parent class and used as a mark of whether to print the log or not, and the log is controlled by the idea of polymorphism, which avoids redundant configuration in the code.
Experimental code
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>
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"or"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);
}
}
In summary, the value taking method is related to the metadata placement location.