获取手机运行内存SD卡等数据

2017-12-28  本文已影响77人  huomenghui

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);

    }

/**

* 获取手机内部剩余存储空间

*

    * @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;

    }

/**

* 获取手机内部总的存储空间

*

    * @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;

        }

}

/**

* 获取系统总内存

*

    * @param context 可传入应用程序上下文。

    * @return 总内存大单位为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;

    }

/**

* 获取当前系统可用内存,返回数据以字节为单位。

*

    * @param context 可传入应用程序上下文。

    * @return 当前可用内存单位为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 转换后的单位

*/

    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是中国联通,03是中国电信。

//        if (IMSI == null || "".equals(IMSI)) {

//            ProvidersName = "获取手机号码失败";

//        } else if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) {

//            ProvidersName = "中国移动";

//        } else if (IMSI.startsWith("46001")) {

//            ProvidersName = "中国联通";

//        } else if (IMSI.startsWith("46003")) {

//            ProvidersName = "中国电信";

//        }

        return IMSI;

    }

上一篇 下一篇

猜你喜欢

热点阅读