Get AndroidManifest meta-data

1

I'm developing a library.

In order to use, the developer must inform a use key.

I would like to use the same form as Google maps:

    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />

Is there any way I can get this information from within AndroidManifest ?

    
asked by anonymous 01.12.2016 / 19:18

1 answer

4

To get the information declared in the meta-data element use the field metaData of class PackageItemInfo .

If you have the following <meta-data>

<meta-data android:name="api_key" android:value="chave123" />

The following code will put in the string apiKey the value "chave123"

try {
    PackageItemInfo packageInfo = getPackageManager().getApplicationInfo(getPackageName(),
                                                             PackageManager.GET_META_DATA);
    Bundle bundle = packageInfo.metaData;
    String apiKey = bundle.getString("api_key");
} catch (PackageManager.NameNotFoundException e) {
    Log.e("MetaData", "Erro ao ler meta-data, NameNotFound: " + e.getMessage());
} catch (NullPointerException e) {
    Log.e("MetaData", "Erro ao ler meta-data, NullPointer: " + e.getMessage());
}
    
01.12.2016 / 21:15