What is metadata ? And what is the use of it in android
Let me give a simple example. It helps in finding additional information -- is the basic explanation of metadata.
317-745-4618
If I add the metadata that it's a phone number than you know or can figure out the geographical area from this. If I did not state that its a phone number than you do not have enough metadata to know what object is this. It could be a US SSN or it may be something else.
It's used in Android to add objects to the manifest, for example if using Google Service one would add it to designate the Google Services version the app binds to.
In Android, you can define meta-data
information in your AndroidManifest.xml
HERE IS THE DOCK LINK
Very basic usage
It is basically an additional option to store information that can be accessed through the entire project. In this case, <meta-data>
is defined outside <activity>
tag and inside <application>
tag.
Defining:
<manifest>
<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<meta-data android:name="my_test_metagadata" android:value="testValue" />
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<manifest>
Reading:
ApplicationInfo ai = getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
Bundle bundle = ai.metaData;
String myApiKey = bundle.getString("my_test_metagadata");
You can save a boolean
, an int
, String
or float.
It's useful for library or APIs
Let's say that you created an API/LIB which can be used for everyone. However, for a specific procedure, you need a KEY
and that KEY
must be defined by the developer who will use your API.
This way, you can not predict which key the developer will share.
Using <meta-data>
, a developer who wants to use your API/LIB can share the KEY
with you. This way, you leave your API configured to read that KEY and raise an exception if the user did not define.
try {
ApplicationInfo ai = getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
Bundle bundle = ai.metaData;
String myApiKey = bundle.getString("my_test_metagadata");
} catch (Exception e) {
Log.e(TAG, "Dear developer. Don't forget to configure <meta-data android:name=\"my_test_metagadata\" android:value=\"testValue\"/> in your AndroidManifest.xml file.");
}
One classic example is Google Ads (Admob).
You must add following line to your AndroidManifest
:
<!--This meta-data tag is required to use Google Play Services. (adMob)-->
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
This will load com.google.android.gms.version
with value represented by @integer/google_play_services_version
. Then, probably, Google Play Services (Admob) will read this metadata and it will be able to determine the version of Google Play Service that you used when you built your app.
Another example
Another usage for <meta-data>
is when to use them to configure an Activity. This way you can pass valuable information to android about your activity, and then Android can handle your activity properly.
In this case, the <meta-data>
tag is added inside the <activity>
tag.
The first example I see is when you define a Search Activity.
<manifest>
<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SearchableActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
</application>
<manifest>
Then, to get the meta-data from the activity tag, use this:
try {
ActivityInfo ai = getPackageManager().getActivityInfo(this.getComponentName(), PackageManager.GET_META_DATA);
Bundle bundle = ai.metaData;
if (bundle != null) {
String apiKey = bundle.getString("apikey");
Log.d(this.getClass().getSimpleName(), "apiKey = " + apiKey);
}
}
} catch (PackageManager.NameNotFoundException e) {
Utilities.log(this.getClass().getSimpleName(), "Failed to load meta-data, NameNotFound: " + e.getMessage());
} catch (NullPointerException e) {
Log.e(this.getClass().getSimpleName(), "Failed to load meta-data, NullPointer: " + e.getMessage());
}
Metadata is data that describes other data to inform other applications how to use the data itself.
Android usage:
Intents are great example for that - If you want to pass data in intents it has to be primitive because Android only have pre-build metadata about those kind of objects. (String and integer have different binary structure that the system know how to work with).
Intents also allow you to build your own metadata to your custom objects via the Parcel class (this process of manually build you own metadata called marshalling)