Android獲取手機(jī)信息的工具類
網(wǎng)上收集的一些獲取收集信息的代碼,制作成一個(gè)工具類,以后可以方便調(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;
/**
* 獲取手機(jī)信息工具類
*
*/
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);
}
/** 獲取手機(jī)號(hào)碼 */
public String getPhoneNumber() {
return tm == null ? null : tm.getLine1Number();
}
/** 獲取網(wǎng)絡(luò)類型(暫時(shí)用不到) */
public int getNetWorkType() {
return tm == null ? 0 : tm.getNetworkType();
}
/** 獲取手機(jī)sim卡的序列號(hào)(IMSI) */
public String getIMSI() {
return tm == null ? null : tm.getSubscriberId();
}
/** 獲取手機(jī)IMEI */
public String getIMEI() {
return tm == null ? null : tm.getDeviceId();
}
/** 獲取手機(jī)型號(hào) */
public static String getModel() {
return android.os.Build.MODEL;
}
/** 獲取手機(jī)品牌 */
public static String getBrand() {
return android.os.Build.BRAND;
}
/** 獲取手機(jī)系統(tǒng)版本 */
public static String getVersion() {
return android.os.Build.VERSION.RELEASE;
}
/** 獲得手機(jī)系統(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或者M(jìn)B,內(nèi)存大小規(guī)格化
}
/** 獲取手機(jī)屏幕寬 */
public int getScreenWidth() {
return act.getWindowManager().getDefaultDisplay().getWidth();
}
/** 獲取手機(jī)屏高寬 */
public int getScreenHeight() {
return act.getWindowManager().getDefaultDisplay().getHeight();
}
/** 獲取應(yīng)用包名 */
public String getPackageName() {
return act.getPackageName();
}
/**
* 獲取手機(jī)MAC地址 只有手機(jī)開(kāi)啟wifi才能獲取到mac地址
*/
public String getMacAddress() {
String result = "";
WifiManager wifiManager = (WifiManager) act.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
result = wifiInfo.getMacAddress();
return result;
}
/**
* 獲取手機(jī)CPU信息 //1-cpu型號(hào) //2-cpu頻率
*/
public String[] getCpuInfo() {
String str1 = "/proc/cpuinfo";
String str2 = "";
String[] cpuInfo = { "", "" }; // 1-cpu型號(hào) //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;
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)微信朋友圈發(fā)本地視頻功能
這篇文章主要介紹了Android實(shí)現(xiàn)微信朋友圈發(fā)本地視頻功能的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-11-11
APP添加CNZZ統(tǒng)計(jì)插件教程 Android版添加phonegap
這篇文章主要介紹了APP添加CNZZ統(tǒng)計(jì)插件教程,Android版添加phonegap,感興趣的小伙伴們可以參考一下2015-12-12
Android利用MediaRecorder實(shí)現(xiàn)錄音功能
這篇文章主要為大家詳細(xì)介紹了Android利用MediaRecorder實(shí)現(xiàn)錄音功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
360瀏覽器文本框獲得焦點(diǎn)后被android軟鍵盤遮罩該怎么辦
最近接了個(gè)項(xiàng)目,項(xiàng)目需求是這樣的,站點(diǎn)上篩選按鈕點(diǎn)擊后彈出層(fixed),當(dāng)輸入框獲取焦點(diǎn)以后彈出系統(tǒng)自帶的軟鍵盤,在android上十款瀏覽器挨個(gè)測(cè)試比對(duì),發(fā)現(xiàn)在360瀏覽器彈出鍵盤以后獲取焦點(diǎn)的文本框被軟鍵盤覆蓋了,下面分享我的解決辦法2015-12-12
Android開(kāi)發(fā)可添加頭尾的RecycleView的實(shí)現(xiàn)
這篇文章主要為大家介紹了Android開(kāi)發(fā)可添加頭尾的RecycleView的實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Android 6.0 無(wú)法在SD卡創(chuàng)建目錄的方法
今天小編就為大家分享一篇Android 6.0 無(wú)法在SD卡創(chuàng)建目錄的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
android聊天界面鍵盤、表情切換絲滑實(shí)現(xiàn)的具體思路
這篇文章主要給大家介紹了關(guān)于android聊天界面鍵盤、表情切換絲滑實(shí)現(xiàn)的具體思路,具體實(shí)現(xiàn)包括在XML布局中使用FrameLayout和RecyclerView,并在代碼中進(jìn)行相應(yīng)的高度控制和事件處理,需要的朋友可以參考下2024-12-12
詳解Android?Flutter如何自定義動(dòng)畫路由
flutter中有默認(rèn)的Route組件,叫做MaterialPageRoute,但是MaterialPageRoute太普通了,如果我們想要做點(diǎn)不同的跳轉(zhuǎn)特效應(yīng)該如何處理呢?一起來(lái)看看吧2023-04-04
Flutter 開(kāi)發(fā)一個(gè)登錄頁(yè)面
登錄頁(yè)面在 App 開(kāi)發(fā)中非常常見(jiàn),本篇借登錄頁(yè)面的開(kāi)發(fā)介紹了文本框 TextField組件的使用,同時(shí)使用文本框的裝飾屬性實(shí)現(xiàn)了個(gè)性化文本框設(shè)置。2021-06-06

