借鑑: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;
}
}