e429383f192a78e002c6d4da7df81bb7d5cea7e2.svn-base 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package eVVM.apk.helper;
  2. import android.content.Context;
  3. import android.content.pm.PackageManager;
  4. import android.os.Bundle;
  5. /**
  6. * desc : 获取清单文件中的值
  7. */
  8. public final class ManifestHelper {
  9. /**
  10. * 获取 meta-data 的值
  11. */
  12. private static Bundle getMetaData(Context context) {
  13. try {
  14. return context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA).metaData;
  15. } catch (PackageManager.NameNotFoundException ignored) {
  16. return new Bundle();
  17. }
  18. }
  19. /**
  20. * 检查 key
  21. * @param context 上下文
  22. * @return meta-data
  23. */
  24. public static Bundle checkMetaData(Context context, String key) {
  25. Bundle metaData = getMetaData(context);
  26. if (!metaData.containsKey(key)) {
  27. // 清单文件没有设置这个 key
  28. throw new IllegalArgumentException("are you ok?");
  29. }
  30. return metaData;
  31. }
  32. /**
  33. * 是否有设置这个值
  34. *
  35. * @param context 上下文
  36. * @param key key
  37. * @return value
  38. */
  39. public static boolean contains(Context context, String key) {
  40. return checkMetaData(context, key).containsKey(key);
  41. }
  42. /**
  43. * 获取 Object
  44. *
  45. * @param context 上下文
  46. * @param key key
  47. * @return value
  48. */
  49. public static Object get(Context context, String key) {
  50. return checkMetaData(context, key).get(key);
  51. }
  52. /**
  53. * 获取 String
  54. *
  55. * @param context 上下文
  56. * @param key key
  57. * @return value
  58. */
  59. public static String getString(Context context, String key) {
  60. return checkMetaData(context, key).getString(key);
  61. }
  62. /**
  63. * 获取 boolean
  64. *
  65. * @param context 上下文
  66. * @param key key
  67. * @return value
  68. */
  69. public static boolean getBoolean(Context context, String key) {
  70. return checkMetaData(context, key).getBoolean(key);
  71. }
  72. /**
  73. * 获取 int
  74. *
  75. * @param context 上下文
  76. * @param key key
  77. * @return value
  78. */
  79. public static int getInt(Context context, String key) {
  80. return checkMetaData(context, key).getInt(key);
  81. }
  82. /**
  83. * 获取 long
  84. *
  85. * @param context 上下文
  86. * @param key key
  87. * @return value
  88. */
  89. public static long getLong(Context context, String key) {
  90. return checkMetaData(context, key).getLong(key);
  91. }
  92. /**
  93. * 获取 float
  94. *
  95. * @param context 上下文
  96. * @param key key
  97. * @return value
  98. */
  99. public static float getFloat(Context context, String key) {
  100. return checkMetaData(context, key).getFloat(key);
  101. }
  102. /**
  103. * 获取 double
  104. *
  105. * @param context 上下文
  106. * @param key key
  107. * @return value
  108. */
  109. public static double getDouble(Context context, String key) {
  110. return checkMetaData(context, key).getDouble(key);
  111. }
  112. }