Android中獲取手機IMEI,IMSI, MAC(Android 6.0)工具類

1. IMEI

IMEI(International Mobile Equipment Identity)是國際移動設備身份碼的縮寫,國際移動裝備辨識碼,是由15位數字組成的"電子串號",它與每臺移動電話機一一對應,而且該碼是全世界唯一的。每一只移動電話機在組裝完成后都將被賦予一個全球唯一的一組號碼,這個號碼從生產到交付使用都將被制造生產的廠商所記錄。

PS:通俗來講就是標識你當前設備(手機)全世界唯一,類似于個人身份證

2. IMSI

國際移動用戶識別碼(IMSI:International Mobile Subscriber Identification

Number)是區別移動用戶的標志,儲存在SIM卡中,可用于區別移動用戶的有效信息。其總長度不超過15位,同樣使用0~9的數字。其中MCC是移動用戶所屬國家代號,占3位數字,中國的MCC規定為460;MNC是移動網號碼,由兩位或者三位數字組成,中國移動的移動網絡編碼(MNC)為00;用于識別移動用戶所歸屬的移動通信網;MSIN是移動用戶識別碼,用以識別某一移動通信網中的移動用戶

PS:通俗來講就是標識你當前SIM卡(手機卡)唯一,同樣類似于個人身份證

3. MAC

MAC(Media Access Control或者Medium Access Control)地址,意譯為媒體訪問控制,或稱為物理地址、硬件地址,用來定義網絡設備的位置。在OSI模型中,第三層網絡層負責 IP地址,第二層數據鏈路層則負責 MAC地址。因此一個主機會有一個MAC地址,而每個網絡位置會有一個專屬于它的IP地址

PS:通俗來講就是標識你當前使用我這個軟件(功能)時的地址

最主要的是:在平板設備上,無法通過imei標示設備,我們會將mac地址作為用戶的唯一標識

下面貼出獲取這三項的代碼。。。

import android.content.Context;

import android.net.wifi.WifiInfo;

import android.net.wifi.WifiManager;

import android.telephony.TelephonyManager;

/**

* 獲取手機信息工具類

* @author zzZ

*/

public class MobileInfoUtil {

/**

* 獲取手機IMEI

* @param context

* @return

*/

public static final String getIMEI(Context context) {

try {

//實例化TelephonyManager對象

TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

//獲取IMEI號

String imei = telephonyManager.getDeviceId();

//在次做個驗證,也不是什么時候都能獲取到的啊

if (imei == null) {

imei = "";

}

return imei;

} catch (Exception e) {

e.printStackTrace();

return "";

}

}

/**

* 獲取手機IMSI

*/

public static String getIMSI(Context context){

try {

TelephonyManager telephonyManager=(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);

//獲取IMSI號

String imsi=telephonyManager.getSubscriberId();

if(null==imsi){

imsi="";

}

return imsi;

} catch (Exception e) {

e.printStackTrace();

return "";

}

}

}

從Android 6.0之后,android 移除了通過 WiFi 和藍牙 API 來在應用程序中可編程的訪問本地硬件標示符。現在 WifiInfo.getMacAddress() 和 BluetoothAdapter.getAddress() 方法都將返回 02:00:00:00:00:00 。

下獲取MAC地址方式,如下:

/**

* 根據手機系統版本獲取MAC地址

* @return

*/

public static String getMAC(Context context) {

String str = null, macSerial = null;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { // 如果當前設備系統大于等于6.0 使用下面的方法

return getHighVersionMac();

} else {

return getLowVersionMac();

}

}

/**

* 獲取MAC地址 針對系統版本大于等于Android 6.0

* @return

*/

public static String getHighVersionMac(Context context) {

String str = "";

String macSerial = "";

try {

Process pp = Runtime.getRuntime().exec(

"cat /sys/class/net/wlan0/address ");

InputStreamReader ir = new InputStreamReader(pp.getInputStream());

LineNumberReader input = new LineNumberReader(ir);

for (; null != str; ) {

str = input.readLine();

if (str != null) {

macSerial = str.trim();// 去空格

break;

}

}

} catch (Exception ex) {

ex.printStackTrace();

}

if (macSerial == null || "".equals(macSerial)) {

try {

return loadFileAsString("/sys/class/net/eth0/address")

.toUpperCase().substring(0, 17);

} catch (Exception e) {

e.printStackTrace();

}

}

return macSerial;

}

public static String loadFileAsString(String fileName) throws Exception {

FileReader reader = new FileReader(fileName);

String text = loadReaderAsString(reader);

reader.close();

return text;

}

/**

* 獲取MAC地址 針對系統版本小于Android 6.0

* @return

*/

public static String getLowVersionMac(Context context) {

try {

WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

// 獲取MAC地址

WifiInfo wifiInfo = wifiManager.getConnectionInfo();

String mac = wifiInfo.getMacAddress();

if (null == mac) {

// 未獲取到

mac = "";

}

return mac;

} catch (Exception e) {

e.printStackTrace();

return "";

}

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容