獲取手機運行內(nèi)存SD卡等數(shù)據(jù)

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;

? ? }

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評論 19 139
  • ¥開啟¥ 【iAPP實現(xiàn)進入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
    小菜c閱讀 7,295評論 0 17
  • 環(huán)信官方Demo源碼分析及SDK簡單應(yīng)用 環(huán)信官方Demo源碼分析及SDK簡單應(yīng)用-ChatDemoUI3.0 環(huán)...
    imGeek閱讀 1,846評論 0 4
  • 今天繼續(xù)《躍遷》第三章聯(lián)機學習。 如何讓知識轉(zhuǎn)化為價值?古典老師開始向商業(yè)思想大師們?nèi)〗?jīng)。他發(fā)現(xiàn)這些大師們都擁有以...
    helenxxf閱讀 195評論 0 1
  • “ 來陪我 ” “ 好 ” 有一段時間里,我常常會翻看很早以前的短信。神經(jīng)兮兮的懷念一下,曾經(jīng)那個言辭間滲透著鄉(xiāng)村...
    曹帽子閱讀 509評論 0 1

友情鏈接更多精彩內(nèi)容