import android.app.ActivityManager;
import android.content.Context;
import android.os.Environment;
import android.os.StatFs;
import android.telephony.TelephonyManager;
import android.util.Log;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
/**
* Created by apple on 2017/9/30.
*/
public class HardwareUtils {
/**
* SD卡是否存在
*/
? ? public static boolean externalMemoryAvailable() {
return android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
? ? }
/**
* 獲取手機內(nèi)部剩余存儲空間
*
? ? * @return b
*/
? ? public static long getAvailableInternalMemorySize() {
File path = Environment.getDataDirectory();
? ? ? ? StatFs stat =new StatFs(path.getPath());
? ? ? ? long blockSize = stat.getBlockSize();
? ? ? ? long availableBlocks = stat.getAvailableBlocks();
? ? ? ? return availableBlocks * blockSize;
? ? }
/**
* 獲取手機內(nèi)部總的存儲空間
*
? ? * @return b
*/
? ? public static long getTotalInternalMemorySize() {
File path = Environment.getDataDirectory();
? ? ? ? StatFs stat =new StatFs(path.getPath());
? ? ? ? long blockSize = stat.getBlockSize();
? ? ? ? long totalBlocks = stat.getBlockCount();
? ? ? ? return totalBlocks * blockSize;
? ? }
private static final int ERROR = -1;//
? ? /**
* 獲取卡剩余存儲空間
*
? ? * @return
? ? */
? ? public static long getAvailableExternalMemorySize() {
if (externalMemoryAvailable()) {
File path = Environment.getExternalStorageDirectory();
? ? ? ? ? ? StatFs stat =new StatFs(path.getPath());
? ? ? ? ? ? long blockSize = stat.getBlockSize();
? ? ? ? ? ? long availableBlocks = stat.getAvailableBlocks();
? ? ? ? ? ? return availableBlocks * blockSize;
? ? ? ? }else {
return ERROR;
? ? ? ? }
}
/**
* 獲取SD卡總的存儲空間
*
? ? * @return
? ? */
? ? public static long getTotalExternalMemorySize() {
if (externalMemoryAvailable()) {
File path = Environment.getExternalStorageDirectory();
? ? ? ? ? ? StatFs stat =new StatFs(path.getPath());
? ? ? ? ? ? long blockSize = stat.getBlockSize();
? ? ? ? ? ? long totalBlocks = stat.getBlockCount();
? ? ? ? ? ? return totalBlocks * blockSize;
? ? ? ? }else {
return ERROR;
? ? ? ? }
}
/**
* 獲取系統(tǒng)總內(nèi)存
*
? ? * @param context 可傳入應(yīng)用程序上下文。
? ? * @return 總內(nèi)存大單位為B。
*/
? ? public static long getTotalMemorySize(Context context) {
String dir ="/proc/meminfo";
? ? ? ? try {
FileReader fr =new FileReader(dir);
? ? ? ? ? ? BufferedReader br =new BufferedReader(fr, 2048);
? ? ? ? ? ? String memoryLine = br.readLine();
? ? ? ? ? ? String subMemoryLine = memoryLine.substring(memoryLine.indexOf("MemTotal:"));
? ? ? ? ? ? br.close();
? ? ? ? ? ? return Integer.parseInt(subMemoryLine.replaceAll("\\D+", "")) *1024l;
? ? ? ? }catch (IOException e) {
e.printStackTrace();
? ? ? ? }
return 0;
? ? }
/**
* 獲取當前系統(tǒng)可用內(nèi)存,返回數(shù)據(jù)以字節(jié)為單位。
*
? ? * @param context 可傳入應(yīng)用程序上下文。
? ? * @return 當前可用內(nèi)存單位為B。
*/
? ? public static long getAvailableMemory(Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
? ? ? ? ActivityManager.MemoryInfo memoryInfo =new ActivityManager.MemoryInfo();
? ? ? ? am.getMemoryInfo(memoryInfo);
? ? ? ? return memoryInfo.availMem;
? ? }
private static DecimalFormatfileIntegerFormat =new DecimalFormat("#0");
? ? private static DecimalFormatfileDecimalFormat =new DecimalFormat("#0.#");
? ? /**
* 單位換算
*
? ? * @param size? ? ? 單位為B
? ? * @param isInteger 是否返回取整的單位
? ? * @return 轉(zhuǎn)換后的單位
*/
? ? public static StringformatFileSize(long size, boolean isInteger) {
DecimalFormat df = isInteger ?fileIntegerFormat :fileDecimalFormat;
? ? ? ? String fileSizeString ="0M";
? ? ? ? if (size <1024 && size >0) {
fileSizeString = df.format((double) size) +"B";
? ? ? ? }else if (size <1024 *1024) {
fileSizeString = df.format((double) size /1024) +"K";
? ? ? ? }else if (size <1024 *1024 *1024) {
fileSizeString = df.format((double) size / (1024 *1024)) +"M";
? ? ? ? }else {
fileSizeString = df.format((double) size / (1024 *1024 *1024)) +"G";
? ? ? ? }
return fileSizeString;
? ? }
/////////////////////////////////////////////////////////
? ? /**
* 獲取dnsIp
? ? * @return
? ? */
? ? public static? StringgetLocalDNS() {
Process cmdProcess =null;
? ? ? ? BufferedReader reader =null;
? ? ? ? String dnsIP ="";
? ? ? ? try {
cmdProcess = Runtime.getRuntime().exec("getprop net.dns1");
? ? ? ? ? ? reader =new BufferedReader(new InputStreamReader(cmdProcess.getInputStream()));
? ? ? ? ? ? dnsIP = reader.readLine();
? ? ? ? ? ? return dnsIP;
? ? ? ? }catch (IOException e) {
return null;
? ? ? ? }finally {
try {
reader.close();
? ? ? ? ? ? }catch (IOException e) {
}
cmdProcess.destroy();
? ? ? ? }
}
////////////////////////////////
? ? /**
* 判斷是否是模擬器
*
*
? ? * @return
? ? */
? ? public static boolean CheckEmulatorBuild() {
String BOARD = android.os.Build.BOARD;
? ? ? ? String BOOTLOADER = android.os.Build.BOOTLOADER;
? ? ? ? String BRAND = android.os.Build.BRAND;
? ? ? ? String DEVICE = android.os.Build.DEVICE;
? ? ? ? String HARDWARE = android.os.Build.HARDWARE;
? ? ? ? String MODEL = android.os.Build.MODEL;
? ? ? ? String PRODUCT = android.os.Build.PRODUCT;
? ? ? ? String SERIAL = android.os.Build.SERIAL;
? ? ? ? /*if (BOARD == "unknown" || BOOTLOADER == "unknown"
|| BRAND == "generic" || DEVICE == "generic"
|| MODEL == "sdk" || PRODUCT == "sdk"
|| HARDWARE == "goldfish" || SERIAL.equals("unknown")
|| BRAND.toLowerCase().equals("android")
|| MODEL.toLowerCase().contains("sdk")) {
Log.v("Result:", "Find Emulator by EmulatorBuild!");
return true;
}*/
? ? ? ? if (BRAND.toLowerCase().equals("android") || MODEL.toLowerCase().contains("sdk")) {
Log.v("Result:", "Find Emulator by EmulatorBuild!");
return true;
? ? ? ? }
Log.v("Result:", "Not Find Emulator by EmulatorBuild!");
return false;
? ? }
///////////////////////////////////////////////////////
? ? /**
* 獲取手機運營商
*
? ? * @param context
? ? * @return
? ? */
? ? public static StringgetProvidersName(Context context) {
TelephonyManager telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
? ? ? ? String ProvidersName =null;
? ? ? ? // 返回唯一的用戶ID;就是這張卡的編號神馬的
? ? ? ? String IMSI = telephonyManager.getSubscriberId();
? ? ? ? // IMSI號前面3位460是國家,緊接著后面2位00 02是中國移動,01是中國聯(lián)通,03是中國電信。
//? ? ? ? if (IMSI == null || "".equals(IMSI)) {
//? ? ? ? ? ? ProvidersName = "獲取手機號碼失敗";
//? ? ? ? } else if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) {
//? ? ? ? ? ? ProvidersName = "中國移動";
//? ? ? ? } else if (IMSI.startsWith("46001")) {
//? ? ? ? ? ? ProvidersName = "中國聯(lián)通";
//? ? ? ? } else if (IMSI.startsWith("46003")) {
//? ? ? ? ? ? ProvidersName = "中國電信";
//? ? ? ? }
? ? ? ? return IMSI;
? ? }