I am new to android and I have not seen or heard about meta data before. However I google it and search about it on YouTube that it is basically a information of your object. Correct me if I am wrong. Can any one help me to understand it in a better way.
1) What is meta data?
2) Why is it used in Android?
It will be good if explanation is given with example of why metadata is used in Android. I have seen them inside manifest's activity metadata tag.
Metadata summarizes basic information about data, making finding & working with particular instances of data easier. Metadata can be created manually to be more accurate, or automatically and contain more basic information.
A simple example of metadata for a document might include a collection of information like the author, file size, the date the document was created, and keywords to describe the document. Metadata for a music file might include the artist's name, the album, and the year it was released.
Metadata represents data about data. Metadata enriches the data with information that makes it easier to find, use and manage. For instance, HTML tags define layout for human readers. Semantic metadata helps computers to interpret data by adding references to concepts in a knowledge graph.
The ModuleMetadata module contains metadata about the list of modules on a given device. The metadata is parsed and cached as soon as the system server starts.
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()); }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With