try {
ApplicationInfo ai = context.getPackageManager().getApplicationInfo(
context.getPackageName(), PackageManager.GET_META_DATA);
if (ai.metaData != null) {
String metaData = ai.metaData.getString("com.example.test.metatest");
}
} catch (PackageManager.NameNotFoundException e) {
// if we can't find it in the manifest, just return null
}
Sometimes you have the need to set up some app-wide configuration information in an Android app or need to create a class that can be used in multiple projects with a generic way of setting configuration values. This is particularly useful for things like API keys that will probably be different across apps but should be accessible in the same way. There are several ways to do it, but the one I’ve come to prefer is adding ameta-data nodeto theAndroidManifest.xmlfile.
This field can be used to store a boolean, float, int, or String and is later accessed by theBundlemethod for your data type (e.g.,getInt()). Here is an example of how to define a value in your AndroidManifest.xml:
ActivityextendsContextWrapperwhich has agetPackageManager()method. That method returns thePackageManager, which is used to fetch theApplicationInfo, passing the package name and the meta-data flag. The returned ApplicationInfo contains a field, metaData, which is actually a Bundle containing all the meta data. Line 4 fetches a String that is the same as the “android:name” parameter in the XML.
That sounds more complicated than it really is. Basically, if you have an Activity, you can fetch the Bundle that has all your meta-data from the AndroidManifest and use it throughout the app.
About Ian G. Clifton
Ian currently works as the Director of User Experience forA.R.O.in Seattle, WA where he also leads Android development. Previously, he has worked as an Android developer, web developer, and even a Satellite, Wideband, and Telemetry Systems Journeyman in the United States Air Force. He has created theEssentials of Android Application Development LiveLessonsvideo series and has just finished his first book,Android User Interface Design: Turning Ideas and Sketches into Beautifully Designed Apps.He spends his off time on photography, drawing, developing, and doing technical review for other Android developers. You can follow his posts on this blog or his ramblings onTwitter.
One Response toUsing Meta-Data In An AndroidManifest
Thiago L. Silvasays:Thanks. Now I know how to use this tag. One question. This tag is like one SharedPreferences , but It can be access by the package of program ?