借鉴:https://www.jianshu.com/p/ca869aa2fd72
今天有两个工具类总结,代码里都有注释,直接看代码。
一、首先第一个,主要获取手机设备信息DeviceInfoUtils。
public class DeviceInfoUtils {
/**
* 获取当前手机系统语言。
*
* @return 返回当前系统语言。例如:当前设置的是“中文-中国”,则返回“zh-CN”
*/
public static String getSystemLanguage() {
return Locale.getDefault().getLanguage();
}
/**
* 获取当前系统上的语言列表(Locale列表)
*/
public static Locale[] getSystemLanguageList() {
return Locale.getAvailableLocales();
}
/**
* 获取当前手机系统版本号
*/
public static String getSystemVersion() {
return Build.VERSION.RELEASE;
}
/**
* 获取手机型号
*/
public static String getSystemModel() {
return Build.MODEL;
}
/**
* 获取设备宽度(px)
*/
public static int getDeviceWidth(Context context) {
return context.getResources().getDisplayMetrics().widthPixels;
}
/**
* 获取设备高度(px)
*/
public static int getDeviceHeight(Context context) {
return context.getResources().getDisplayMetrics().heightPixels;
}
/**
* 获取手机IMEI(需要“android.permission.READ_PHONE_STATE”权限)
*/
@SuppressLint("MissingPermission")
public static String getIMEI(Context ctx) {
TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Activity.TELEPHONY_SERVICE);
if (tm != null) {
return tm.getDeviceId();
}
return null;
}
/**
* 获取厂商名
*/
public static String getDeviceManufacturer() {
return Build.MANUFACTURER;
}
/**
* 获取产品名
*/
public static String getDeviceProduct() {
return Build.PRODUCT;
}
/**
* 获取手机品牌
*/
public static String getDeviceBrand() {
return Build.BRAND;
}
/**
* 获取手机型号
*/
public static String getDeviceModel() {
return Build.MODEL;
}
/**
* 获取手机主板名
*/
public static String getDeviceBoard() {
return Build.BOARD;
}
/**
* 设备名
*/
public static String getDeviceDevice() {
return Build.DEVICE;
}
/**
* 手机Fingerprint标识
* fingerprit 信息
*/
public static String getDeviceFubgerprint() {
return Build.FINGERPRINT;
}
/**
* 硬件名
*/
public static String getDeviceHardware() {
return Build.HARDWARE;
}
/**
* 主机
*/
public static String getDeviceHost() {
return Build.HOST;
}
/**
* 显示ID
*/
public static String getDeviceDisplay() {
return Build.DISPLAY;
}
/**
* ID
*/
public static String getDeviceId() {
return Build.ID;
}
/**
* 获取手机用户名
*/
public static String getDeviceUser() {
return Build.USER;
}
/**
* 获取手机 硬件序列号
*/
public static String getDeviceSerial() {
return Build.SERIAL;
}
/**
* 获取手机Android系统SDK
*/
public static int getDeviceSDK() {
return Build.VERSION.SDK_INT;
}
/**
* Codename
*/
public static String getDeviceCodename(){
return Build.VERSION.CODENAME;
}
/**
* Bootloader
*/
public static String getDeviceBootloader(){
return Build.BOOTLOADER;
}
/**
* 版本类型
*/
public static String getType(){
return Build.TYPE;
}
/**
* 安全patch 时间
*/
public static String getSecurityPatch(){
return Build.VERSION.SECURITY_PATCH;
}
}
二、第二个主要获取RAM、ROM存储信息SDCardUtils。
public class SDCardUtils {
private static final int INTERNAL_STORAGE = 0;
private static final int EXTERNAL_STORAGE = 1;
/**
* 判断外部存储SD是否可用 (存在且具有读写权限)
*/
public static boolean isSDCardMount() {
return Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED);
}
/**
* 获取手机存储 ROM 信息
*
* type: 用于区分内置存储与外置存储的方法
*
* 内置SD卡 :INTERNAL_STORAGE = 0;
*
* 外置SD卡: EXTERNAL_STORAGE = 1;
*/
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public static String getStorageInfo(Context context, int type) {
String path = getStoragePath(context, type);
/**
* 无外置SD 卡判断
*/
if (isSDCardMount() == false || TextUtils.isEmpty(path) || path == null) {
return "无外置SD卡";
}
File file = new File(path);
StatFs statFs = new StatFs(file.getPath());
String stotageInfo;
long blockCount = statFs.getBlockCountLong();
long bloackSize = statFs.getBlockSizeLong();
long totalSpace = bloackSize * blockCount;
long availableBlocks = statFs.getAvailableBlocksLong();
long availableSpace = availableBlocks * bloackSize;
stotageInfo = "可用/总共:"
+ Formatter.formatFileSize(context, availableSpace) + "/"
+ Formatter.formatFileSize(context, totalSpace);
return stotageInfo;
}
/**
* 获取 手机 RAM 信息
*/
public static String getRAMInfo(Context context) {
long totalSize = 0;
long availableSize = 0;
ActivityManager activityManager = (ActivityManager) context
.getSystemService(context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);
totalSize = memoryInfo.totalMem;
availableSize = memoryInfo.availMem;
return "可用/总共:" + Formatter.formatFileSize(context, availableSize)
+ "/" + Formatter.formatFileSize(context, totalSize);
}
/**
* 获取 手机 RAM 存储空间
*/
public static String getTotalRAM(Context context) {
long size = 0;
ActivityManager activityManager = (ActivityManager) context
.getSystemService(context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo outInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(outInfo);
size = outInfo.totalMem;
return Formatter.formatFileSize(context, size);
}
/**
* 获取 手机 可用 RAM
*/
public static String getAvailableRAM(Context context) {
long size = 0;
ActivityManager activityManager = (ActivityManager) context
.getSystemService(context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo outInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(outInfo);
size = outInfo.availMem;
return Formatter.formatFileSize(context, size);
}
/**
* 获取手机ROM存储空间
*
* @param context
* @return 以M,G为单位的容量
*/
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public static String getTotalExternalMemorySize(Context context) {
File file = Environment.getExternalStorageDirectory();
StatFs statFs = new StatFs(file.getPath());
long blockSizeLong = statFs.getBlockSizeLong();
long blockCountLong = statFs.getBlockCountLong();
return Formatter
.formatFileSize(context, blockCountLong * blockSizeLong);
}
/**
* 获取手机ROM可用存储空间
*
* @param context
* @return 以M,G为单位的容量
*/
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public static String getAvailableExternalMemorySize(Context context) {
File file = Environment.getExternalStorageDirectory();
StatFs statFs = new StatFs(file.getPath());
long availableBlocksLong = statFs.getAvailableBlocksLong();
long blockSizeLong = statFs.getBlockSizeLong();
return Formatter.formatFileSize(context, availableBlocksLong
* blockSizeLong);
}
//添加由某某启动的SDCard总存储
public static String getSDCardTotalStorage(long totalByte) {
double byte2GB = totalByte / 1024.00 / 1024.00 / 1024.00;
double totalStorage;
if (byte2GB > 1) {
totalStorage = Math.ceil(byte2GB);
if (totalStorage > 1 && totalStorage < 3) {
return 2.0 + "GB";
} else if (totalStorage > 2 && totalStorage < 5) {
return 4.0 + "GB";
} else if (totalStorage >= 5 && totalStorage < 10) {
return 8.0 + "GB";
} else if (totalStorage >= 10 && totalStorage < 18) {
return 16.0 + "GB";
} else if (totalStorage >= 18 && totalStorage < 34) {
return 32.0 + "GB";
} else if (totalStorage >= 34 && totalStorage < 50) {
return 48.0 + "GB";
} else if (totalStorage >= 50 && totalStorage < 66) {
return 64.0 + "GB";
} else if (totalStorage >= 66 && totalStorage < 130) {
return 128.0 + "GB";
}
} else {
// below 1G return get values
totalStorage = totalByte / 1024.00 / 1024.00;
if (totalStorage >= 515 && totalStorage < 1024) {
return 1 + "GB";
} else if (totalStorage >= 260 && totalStorage < 515) {
return 512 + "MB";
} else if (totalStorage >= 130 && totalStorage < 260) {
return 256 + "MB";
} else if (totalStorage > 70 && totalStorage < 130) {
return 128 + "MB";
} else if (totalStorage > 50 && totalStorage < 70) {
return 64 + "MB";
}
}
return totalStorage + "GB";
}
/**
* 使用反射方法 获取手机存储路径
*/
public static String getStoragePath(Context context, int type) {
StorageManager sm = (StorageManager) context
.getSystemService(Context.STORAGE_SERVICE);
try {
Method getPathsMethod = sm.getClass().getMethod("getVolumePaths",null);
String[] path = (String[]) getPathsMethod.invoke(sm, null);
switch (type) {
case INTERNAL_STORAGE:
return path[type];
case EXTERNAL_STORAGE:
if (path.length > 1) {
return path[type];
} else {
return null;
}
default:
break;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}