Android獲取手機信息的工具類
更新時間:2018年09月24日 10:06:15 作者:676598624
這篇文章主要為大家詳細介紹了Android獲取手機信息的工具類,具有一定的參考價值,感興趣的小伙伴們可以參考一下
網(wǎng)上收集的一些獲取收集信息的代碼,制作成一個工具類,以后可以方便調(diào)用。
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import android.app.Activity; import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.provider.Settings; import android.telephony.TelephonyManager; import android.text.format.Formatter; /** * 獲取手機信息工具類 * */ public class PhoneUtil { private static PhoneUtil instance; private TelephonyManager tm; private Activity act; private PhoneUtil(Activity act) { tm = (TelephonyManager) act.getSystemService(Context.TELEPHONY_SERVICE); this.act = act; } public static PhoneUtil getInstance(Activity act) { if (instance == null) { instance = new PhoneUtil(act); } else if (instance.act != act) { instance = new PhoneUtil(act); } return instance; } /** 是否處于飛行模式 */ public boolean isAirModeOpen() { return (Settings.System.getInt(act.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) == 1 ? true : false); } /** 獲取手機號碼 */ public String getPhoneNumber() { return tm == null ? null : tm.getLine1Number(); } /** 獲取網(wǎng)絡類型(暫時用不到) */ public int getNetWorkType() { return tm == null ? 0 : tm.getNetworkType(); } /** 獲取手機sim卡的序列號(IMSI) */ public String getIMSI() { return tm == null ? null : tm.getSubscriberId(); } /** 獲取手機IMEI */ public String getIMEI() { return tm == null ? null : tm.getDeviceId(); } /** 獲取手機型號 */ public static String getModel() { return android.os.Build.MODEL; } /** 獲取手機品牌 */ public static String getBrand() { return android.os.Build.BRAND; } /** 獲取手機系統(tǒng)版本 */ public static String getVersion() { return android.os.Build.VERSION.RELEASE; } /** 獲得手機系統(tǒng)總內(nèi)存 */ public String getTotalMemory() { String str1 = "/proc/meminfo";// 系統(tǒng)內(nèi)存信息文件 String str2; String[] arrayOfString; long initial_memory = 0; try { FileReader localFileReader = new FileReader(str1); BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192); str2 = localBufferedReader.readLine();// 讀取meminfo第一行,系統(tǒng)總內(nèi)存大小 arrayOfString = str2.split("\\s+"); initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;// 獲得系統(tǒng)總內(nèi)存,單位是KB,乘以1024轉(zhuǎn)換為Byte localBufferedReader.close(); } catch (IOException e) { } return Formatter.formatFileSize(act, initial_memory);// Byte轉(zhuǎn)換為KB或者MB,內(nèi)存大小規(guī)格化 } /** 獲取手機屏幕寬 */ public int getScreenWidth() { return act.getWindowManager().getDefaultDisplay().getWidth(); } /** 獲取手機屏高寬 */ public int getScreenHeight() { return act.getWindowManager().getDefaultDisplay().getHeight(); } /** 獲取應用包名 */ public String getPackageName() { return act.getPackageName(); } /** * 獲取手機MAC地址 只有手機開啟wifi才能獲取到mac地址 */ public String getMacAddress() { String result = ""; WifiManager wifiManager = (WifiManager) act.getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); result = wifiInfo.getMacAddress(); return result; } /** * 獲取手機CPU信息 //1-cpu型號 //2-cpu頻率 */ public String[] getCpuInfo() { String str1 = "/proc/cpuinfo"; String str2 = ""; String[] cpuInfo = { "", "" }; // 1-cpu型號 //2-cpu頻率 String[] arrayOfString; try { FileReader fr = new FileReader(str1); BufferedReader localBufferedReader = new BufferedReader(fr, 8192); str2 = localBufferedReader.readLine(); arrayOfString = str2.split("\\s+"); for (int i = 2; i < arrayOfString.length; i++) { cpuInfo[0] = cpuInfo[0] + arrayOfString[i] + " "; } str2 = localBufferedReader.readLine(); arrayOfString = str2.split("\\s+"); cpuInfo[1] += arrayOfString[2]; localBufferedReader.close(); } catch (IOException e) { } return cpuInfo; } /** 獲取Application中的meta-data內(nèi)容 */ public String getMetaData(String name) { String result = ""; try { ApplicationInfo appInfo = act.getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA); result = appInfo.metaData.getString(name); } catch (Exception e) { e.printStackTrace(); } return result; } }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android RecyclerView多類型布局卡片解決方案
這篇文章主要介紹了Android RecyclerView多類型布局卡片解決方案,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03Android studio 連接手機調(diào)試操作步驟
這篇文章主要介紹了Android studio 連接手機調(diào)試操作步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-05-05Android利用Fragment實現(xiàn)Tab選項卡效果
這篇文章主要為大家詳細介紹了Android利用Fragment實現(xiàn)Tab選項卡效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-08-08android 仿微信demo——微信通訊錄界面功能實現(xiàn)(移動端,服務端)
本系列文章主要介紹了微信小程序-閱讀小程序?qū)嵗╠emo),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望能給你們提供幫助2021-06-06淺談RecyclerView(完美替代ListView,GridView)
RecyclerView絕對是一款功能強大的控件,涵蓋了ListView,GridView,瀑布流等數(shù)據(jù)表現(xiàn)的形式。本文對其進行系統(tǒng)介紹,有需要的朋友可以看下2016-12-12