简体   繁体   中英

How to get a long number of metadata tags from AndroidManifest.xml

I want to get a sting frome metadata tags, but i can't use metadata.getString(), becauese it returns null. so, i use metadata.getInt(), but i can't get the correct value. How can i do it? <meta-data android:name="GDT_BANNER_POST_ID" android:value="9079537218417626401"/>

Use this :

ActivityInfo app = getPackageManager().getActivityInfo(this.getComponentName(), PackageManager.GET_ACTIVITIES|PackageManager.GET_META_DATA);
Bundle bundle = ai.metaData;
Object value = (Object)metaData.get(name);

Use a method, such as:

    private String retrieveKey(Context aContext, String aKey) {

    // get the key from the manifest 
    final PackageManager pm = aContext.getPackageManager();
    try { 
        final ApplicationInfo info = pm.getApplicationInfo(aContext.getPackageName(),
                PackageManager.GET_META_DATA);
        if (info.metaData == null) {
            Log.i("Key not found in manifest", aKey);
        } else { 
            final String value = String.valueOf(info.metaData.get(aKey));
            if (value == null) {
                Log.i("Key not found in manifest", aKey);
            } else { 
                return value.trim();
            } 
        } 
    } catch (final PackageManager.NameNotFoundException e) {
        Log.i("Key not found in manifest", aKey);
    } 
    return ""; 
} 

In your manifest, add:

<meta-data
            android:name="MY_KEY"
            android:value="MY_VALUES" />

In your code, just retrieve it:

String myKey = retrieveKey(this, "MY_KEY");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM