Android How to - Get meta data from Context








Question

We would like to know how to get meta data from Context.

Answer

// www .  j  a v a  2s.c om
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;


public class Main {
  public static Object getMetaValue(Context context, String metaKey) {
    Bundle metaData    = null;
        Object metaValue  = null;
        if (context == null || metaKey == null) {
            return null;
        }
        
        try {
            ApplicationInfo ai = context.getPackageManager().getApplicationInfo(
                            context.getPackageName(), PackageManager.GET_META_DATA);
            
            if (null == ai) {
              return null;
            }
            metaData = ai.metaData;
            
            if (null == metaData) {
              return null;
            }
            
            metaValue = metaData.get(metaKey);
        } catch (NameNotFoundException e) {
        }
        return metaValue;
    }
}