Get meta-data from Androidmanifest

Asked

Viewed 139 times

1

I’m developing a library.

For use the developer must inform a usage key.

I would like to use the same way 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 inside AndroidManifest?

1 answer

4


To obtain the information stated in the element meta-date use the field Metadata class Packageiteminfo.

If you have the following <meta-data>

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

the next code will place 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());
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.