Android 获取AndroidManifest.xml 中 meta-data 的值

    public static int GetMetaDataInt(Context context, String name) {
            int Value = -1;
           PackageManager pm = context.getPackageManager();
           ApplicationInfo appInfo;
           try {
                appInfo = pm.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA );
                Value = appInfo. metaData.getInt(name);
           } catch (NameNotFoundException e) {
                e. printStackTrace();
           }
           return Value;
     }

     public static String GetMetaDataString(Context context, String name) {
           String s = "";
           PackageManager pm = context.getPackageManager();
           ApplicationInfo appInfo;
           try {
                appInfo = pm.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA );
                s = appInfo. metaData.getString(name);
           } catch (NameNotFoundException e) {
                e. printStackTrace();
           }
           return s;
     }
<meta-data
                 android:name="ad_banner"
                 android:value="9079537189894199745" />
          如果 meta-data的值为 长数字,可以加 "\0" 强转成字符串,如 将"9079537189894199745"写成"\09079537189894199745"(加"\0")强转成字符串,用getString能拿到正确值。

你可能感兴趣的:(android,meta-data)