What Is Metadata ? and What Is The Use of It in Android

What is metadata ? And what is the use of it in android

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());
}

The use of the android:value in metadata -- Android

meta-data is a name-value pair, so the meaning of android:value is dependent on android:name.
For your example:

<meta-data
android:name="android.support.PARENT_ACTIVITY" //the activity has a parent activity
android:value=".SecondActivity" //the parent activity name is SecondActivity
/>

and what's parent Activity and how to use it ,you could ref https://developer.android.com/training/implementing-navigation/ancestral#top_of_page

if you only provides android:name but no android:value, its pointless.

How to write meta-data info to Android Manifest at runtime

You can't edit the manifest at runtime.

If you need runtime solution, you should try other ways like share preference or something can save value persist.

If not just put it in manifest, take google play service for Example:

<application>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
.....
</application>

No `meta-data android:name=flutterEmbedding android:value=2/` in ..\src\main\AndroidManifest.xml

The problem was that the AndroidManifest.xml file was lacking a required <meta-data android:name="flutterEmbedding" android:value="2"/> tag. Add it like so:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.coding.informer.simple_material_app">

<uses-permission android:name="android.permission.INTERNET"/>
<meta-data android:name="flutterEmbedding" android:value="2"/>

<application android:name="${applicationName}" android:label="simple_material_app" android:icon="@mipmap/ic_launcher">
<activity android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="@android:style/Theme.Black.NoTitleBar"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>


Related Topics



Leave a reply



Submit