当前位置:   article > 正文

Android中获取手机设备信息、RAM、ROM存储信息,如宽、高、厂商名、手机品牌

Android中获取手机设备信息、RAM、ROM存储信息,如宽、高、厂商名、手机品牌

借鉴:https://www.jianshu.com/p/ca869aa2fd72

今天有两个工具类总结,代码里都有注释,直接看代码。

一、首先第一个,主要获取手机设备信息DeviceInfoUtils。

  1. public class DeviceInfoUtils {
  2. /**
  3. * 获取当前手机系统语言。
  4. *
  5. * @return 返回当前系统语言。例如:当前设置的是“中文-中国”,则返回“zh-CN”
  6. */
  7. public static String getSystemLanguage() {
  8. return Locale.getDefault().getLanguage();
  9. }
  10. /**
  11. * 获取当前系统上的语言列表(Locale列表)
  12. */
  13. public static Locale[] getSystemLanguageList() {
  14. return Locale.getAvailableLocales();
  15. }
  16. /**
  17. * 获取当前手机系统版本号
  18. */
  19. public static String getSystemVersion() {
  20. return Build.VERSION.RELEASE;
  21. }
  22. /**
  23. * 获取手机型号
  24. */
  25. public static String getSystemModel() {
  26. return Build.MODEL;
  27. }
  28. /**
  29. * 获取设备宽度(px)
  30. */
  31. public static int getDeviceWidth(Context context) {
  32. return context.getResources().getDisplayMetrics().widthPixels;
  33. }
  34. /**
  35. * 获取设备高度(px)
  36. */
  37. public static int getDeviceHeight(Context context) {
  38. return context.getResources().getDisplayMetrics().heightPixels;
  39. }
  40. /**
  41. * 获取手机IMEI(需要“android.permission.READ_PHONE_STATE”权限)
  42. */
  43. @SuppressLint("MissingPermission")
  44. public static String getIMEI(Context ctx) {
  45. TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Activity.TELEPHONY_SERVICE);
  46. if (tm != null) {
  47. return tm.getDeviceId();
  48. }
  49. return null;
  50. }
  51. /**
  52. * 获取厂商名
  53. */
  54. public static String getDeviceManufacturer() {
  55. return Build.MANUFACTURER;
  56. }
  57. /**
  58. * 获取产品名
  59. */
  60. public static String getDeviceProduct() {
  61. return Build.PRODUCT;
  62. }
  63. /**
  64. * 获取手机品牌
  65. */
  66. public static String getDeviceBrand() {
  67. return Build.BRAND;
  68. }
  69. /**
  70. * 获取手机型号
  71. */
  72. public static String getDeviceModel() {
  73. return Build.MODEL;
  74. }
  75. /**
  76. * 获取手机主板名
  77. */
  78. public static String getDeviceBoard() {
  79. return Build.BOARD;
  80. }
  81. /**
  82. * 设备名
  83. */
  84. public static String getDeviceDevice() {
  85. return Build.DEVICE;
  86. }
  87. /**
  88. * 手机Fingerprint标识
  89. * fingerprit 信息
  90. */
  91. public static String getDeviceFubgerprint() {
  92. return Build.FINGERPRINT;
  93. }
  94. /**
  95. * 硬件名
  96. */
  97. public static String getDeviceHardware() {
  98. return Build.HARDWARE;
  99. }
  100. /**
  101. * 主机
  102. */
  103. public static String getDeviceHost() {
  104. return Build.HOST;
  105. }
  106. /**
  107. * 显示ID
  108. */
  109. public static String getDeviceDisplay() {
  110. return Build.DISPLAY;
  111. }
  112. /**
  113. * ID
  114. */
  115. public static String getDeviceId() {
  116. return Build.ID;
  117. }
  118. /**
  119. * 获取手机用户名
  120. */
  121. public static String getDeviceUser() {
  122. return Build.USER;
  123. }
  124. /**
  125. * 获取手机 硬件序列号
  126. */
  127. public static String getDeviceSerial() {
  128. return Build.SERIAL;
  129. }
  130. /**
  131. * 获取手机Android系统SDK
  132. */
  133. public static int getDeviceSDK() {
  134. return Build.VERSION.SDK_INT;
  135. }
  136. /**
  137. * Codename
  138. */
  139. public static String getDeviceCodename(){
  140. return Build.VERSION.CODENAME;
  141. }
  142. /**
  143. * Bootloader
  144. */
  145. public static String getDeviceBootloader(){
  146. return Build.BOOTLOADER;
  147. }
  148. /**
  149. * 版本类型
  150. */
  151. public static String getType(){
  152. return Build.TYPE;
  153. }
  154. /**
  155. * 安全patch 时间
  156. */
  157. public static String getSecurityPatch(){
  158. return Build.VERSION.SECURITY_PATCH;
  159. }
  160. }

二、第二个主要获取RAM、ROM存储信息SDCardUtils。

  1. public class SDCardUtils {
  2. private static final int INTERNAL_STORAGE = 0;
  3. private static final int EXTERNAL_STORAGE = 1;
  4. /**
  5. * 判断外部存储SD是否可用 (存在且具有读写权限)
  6. */
  7. public static boolean isSDCardMount() {
  8. return Environment.getExternalStorageState().equals(
  9. Environment.MEDIA_MOUNTED);
  10. }
  11. /**
  12. * 获取手机存储 ROM 信息
  13. *
  14. * type: 用于区分内置存储与外置存储的方法
  15. *
  16. * 内置SD卡 :INTERNAL_STORAGE = 0;
  17. *
  18. * 外置SD卡: EXTERNAL_STORAGE = 1;
  19. */
  20. @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
  21. public static String getStorageInfo(Context context, int type) {
  22. String path = getStoragePath(context, type);
  23. /**
  24. * 无外置SD 卡判断
  25. */
  26. if (isSDCardMount() == false || TextUtils.isEmpty(path) || path == null) {
  27. return "无外置SD卡";
  28. }
  29. File file = new File(path);
  30. StatFs statFs = new StatFs(file.getPath());
  31. String stotageInfo;
  32. long blockCount = statFs.getBlockCountLong();
  33. long bloackSize = statFs.getBlockSizeLong();
  34. long totalSpace = bloackSize * blockCount;
  35. long availableBlocks = statFs.getAvailableBlocksLong();
  36. long availableSpace = availableBlocks * bloackSize;
  37. stotageInfo = "可用/总共:"
  38. + Formatter.formatFileSize(context, availableSpace) + "/"
  39. + Formatter.formatFileSize(context, totalSpace);
  40. return stotageInfo;
  41. }
  42. /**
  43. * 获取 手机 RAM 信息
  44. */
  45. public static String getRAMInfo(Context context) {
  46. long totalSize = 0;
  47. long availableSize = 0;
  48. ActivityManager activityManager = (ActivityManager) context
  49. .getSystemService(context.ACTIVITY_SERVICE);
  50. ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
  51. activityManager.getMemoryInfo(memoryInfo);
  52. totalSize = memoryInfo.totalMem;
  53. availableSize = memoryInfo.availMem;
  54. return "可用/总共:" + Formatter.formatFileSize(context, availableSize)
  55. + "/" + Formatter.formatFileSize(context, totalSize);
  56. }
  57. /**
  58. * 获取 手机 RAM 存储空间
  59. */
  60. public static String getTotalRAM(Context context) {
  61. long size = 0;
  62. ActivityManager activityManager = (ActivityManager) context
  63. .getSystemService(context.ACTIVITY_SERVICE);
  64. ActivityManager.MemoryInfo outInfo = new ActivityManager.MemoryInfo();
  65. activityManager.getMemoryInfo(outInfo);
  66. size = outInfo.totalMem;
  67. return Formatter.formatFileSize(context, size);
  68. }
  69. /**
  70. * 获取 手机 可用 RAM
  71. */
  72. public static String getAvailableRAM(Context context) {
  73. long size = 0;
  74. ActivityManager activityManager = (ActivityManager) context
  75. .getSystemService(context.ACTIVITY_SERVICE);
  76. ActivityManager.MemoryInfo outInfo = new ActivityManager.MemoryInfo();
  77. activityManager.getMemoryInfo(outInfo);
  78. size = outInfo.availMem;
  79. return Formatter.formatFileSize(context, size);
  80. }
  81. /**
  82. * 获取手机ROM存储空间
  83. *
  84. * @param context
  85. * @return 以M,G为单位的容量
  86. */
  87. @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
  88. public static String getTotalExternalMemorySize(Context context) {
  89. File file = Environment.getExternalStorageDirectory();
  90. StatFs statFs = new StatFs(file.getPath());
  91. long blockSizeLong = statFs.getBlockSizeLong();
  92. long blockCountLong = statFs.getBlockCountLong();
  93. return Formatter
  94. .formatFileSize(context, blockCountLong * blockSizeLong);
  95. }
  96. /**
  97. * 获取手机ROM可用存储空间
  98. *
  99. * @param context
  100. * @return 以M,G为单位的容量
  101. */
  102. @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
  103. public static String getAvailableExternalMemorySize(Context context) {
  104. File file = Environment.getExternalStorageDirectory();
  105. StatFs statFs = new StatFs(file.getPath());
  106. long availableBlocksLong = statFs.getAvailableBlocksLong();
  107. long blockSizeLong = statFs.getBlockSizeLong();
  108. return Formatter.formatFileSize(context, availableBlocksLong
  109. * blockSizeLong);
  110. }
  111. //添加由某某启动的SDCard总存储
  112. public static String getSDCardTotalStorage(long totalByte) {
  113. double byte2GB = totalByte / 1024.00 / 1024.00 / 1024.00;
  114. double totalStorage;
  115. if (byte2GB > 1) {
  116. totalStorage = Math.ceil(byte2GB);
  117. if (totalStorage > 1 && totalStorage < 3) {
  118. return 2.0 + "GB";
  119. } else if (totalStorage > 2 && totalStorage < 5) {
  120. return 4.0 + "GB";
  121. } else if (totalStorage >= 5 && totalStorage < 10) {
  122. return 8.0 + "GB";
  123. } else if (totalStorage >= 10 && totalStorage < 18) {
  124. return 16.0 + "GB";
  125. } else if (totalStorage >= 18 && totalStorage < 34) {
  126. return 32.0 + "GB";
  127. } else if (totalStorage >= 34 && totalStorage < 50) {
  128. return 48.0 + "GB";
  129. } else if (totalStorage >= 50 && totalStorage < 66) {
  130. return 64.0 + "GB";
  131. } else if (totalStorage >= 66 && totalStorage < 130) {
  132. return 128.0 + "GB";
  133. }
  134. } else {
  135. // below 1G return get values
  136. totalStorage = totalByte / 1024.00 / 1024.00;
  137. if (totalStorage >= 515 && totalStorage < 1024) {
  138. return 1 + "GB";
  139. } else if (totalStorage >= 260 && totalStorage < 515) {
  140. return 512 + "MB";
  141. } else if (totalStorage >= 130 && totalStorage < 260) {
  142. return 256 + "MB";
  143. } else if (totalStorage > 70 && totalStorage < 130) {
  144. return 128 + "MB";
  145. } else if (totalStorage > 50 && totalStorage < 70) {
  146. return 64 + "MB";
  147. }
  148. }
  149. return totalStorage + "GB";
  150. }
  151. /**
  152. * 使用反射方法 获取手机存储路径
  153. */
  154. public static String getStoragePath(Context context, int type) {
  155. StorageManager sm = (StorageManager) context
  156. .getSystemService(Context.STORAGE_SERVICE);
  157. try {
  158. Method getPathsMethod = sm.getClass().getMethod("getVolumePaths",null);
  159. String[] path = (String[]) getPathsMethod.invoke(sm, null);
  160. switch (type) {
  161. case INTERNAL_STORAGE:
  162. return path[type];
  163. case EXTERNAL_STORAGE:
  164. if (path.length > 1) {
  165. return path[type];
  166. } else {
  167. return null;
  168. }
  169. default:
  170. break;
  171. }
  172. } catch (Exception e) {
  173. e.printStackTrace();
  174. }
  175. return null;
  176. }
  177. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/242140
推荐阅读
相关标签
  

闽ICP备14008679号