安卓系統4.0/5.0/6.0獲取單卡,雙卡手機的imei1,imei2,meid
最近這3天,一直在做獲取手機的,imei1,imei2,meid,sn等手機系統資訊的小app,在做的過程中,遇到了很多問題,發現網路上對這塊的技術帖子,並不多,關鍵是還不詳細,有的帖子三言兩語,幾句話,其實說的還是4.0版本的獲取,獲取了imei 獲取不到meid,有的帖子貼了一堆程式碼,其實也並沒有什麼卵用,有的帖子一長串反射程式碼,也是無關痛癢, 所以今天我想把我這3天的心得,分享出來,也想讓廣大開發者,提供點個人的幫助!開始之前,這是方法getDeviceId()的原始碼,這串英文 我就不翻譯了,自己看,網上大部分帖子都是說用這個,其實這個只是4.0的時候拿的。但是4.0的手機也有雙卡手機,所以這就有坑了市面上的安卓手機,有很多種,系統也有各種各樣,有的4.0左右系統,有的5.0左右系統,有的6.0左右系統,有單卡槽的,有雙卡槽的,有全網通的,有卡槽一個是uim一個是 Sim的,有雙SIM的現在我講圖文並茂展示下,我手上測試機,測試後的情況
1.魅族PRO 6手機 雙卡雙待 ,卡1卡2均為SIM卡,系統是基於安卓5.0的 有自己的SIM下面的圖,是我自己寫的安卓程式碼得到的下面圖。是關於設定裡面查詢的這是執行的GIF動態圖
2.手機是魅族 pro6 plus雙卡手機,它是沒有MEID 的,手機系統是5.0+
3.手機是最老的 魅族not 系統是4.0+,雙卡手機 卡1是UIM 卡2是SIM
總結以上:基於安卓系統。4.0 5.0 6.0的系統4.0的系統如果想獲取MEID/IMEI1/IMEI2 ----其實是很難做到的。 因為你只能只用getDeviceId() 這個方法5.0的系統如果想獲取MEID/IMEI1/IMEI2 ----framework層提供了兩個屬性值“ril.cdma.meid"和“ril.gsm.imei"獲取meid、imei1、imei2(5.0的時候如果想要獲取,必須使用反射方法,去拿,下面慢慢看。我會把程式碼貼出來)6.0的系統如果想獲取MEID/IMEI1/IMEI2 ---- 直接獲取系統api getDeviceId(int slotId) //指定slotId的deviceId getDeviceId(0) getDeviceId(1) 但是全網通手機Phone在初始化時只建立了GSMPhone,並沒有建立CDMAPhone,此時是無法獲取到meid。只有插入CDMA卡才會通過PhoneProxy的deleteAndCreatePhone方法,將其中一個phone刪除,重新建立CDMALTEPhone。但是,由於支援盲插功能,我們並不知道使用者會將cdma卡插入哪個卡槽,CDMALTEPhone有可能是卡1,也有可能是卡2。因此使用google原生介面獲取deviceId會出現如下三種情況:
1、不插卡(或兩張卡都是GSM卡) getDeviceId() 返回 imei1 getDeviceId(0) 返回 imei1 getDeviceId(1) 返回 imei2
2、卡1插CDMA卡,卡2不插卡(或卡2插GSM卡) getDeviceId() 返回 meid getDeviceId(0) 返回 meid getDeviceId(1) 返回 imei2
3、卡2插CDMA卡,卡1不插卡(或卡1插GSM卡) getDeviceId() 返回 imei1 getDeviceId(0) 返回 imei1 getDeviceId(1) 返回 meid好吧,上面羅裡吧嗦的說了一堆, 我就直接上程式碼了
if (Build.VERSION.SDK_INT < 21) {
//如果獲取系統的IMEI/MEID,14位代表meid 15位是imei
if (GetSystemInfoUtil.getNumber(getActivity()) == 14) {
mTvPhoneMeid.setText(GetSystemInfoUtil.getImeiOrMeid(getActivity()));//meid
} else if (GetSystemInfoUtil.getNumber(getActivity()) == 15) {
mTvPhoneImei.setText(GetSystemInfoUtil.getImeiOrMeid(getActivity()));//imei1
}
// 21版本是5.0,判斷是否是5.0以上的系統 5.0系統直接獲取IMEI1,IMEI2,MEID
} else if (Build.VERSION.SDK_INT >= 21 && Build.VERSION.SDK_INT<23) {
mTvPhoneImei.setText(GetSystemInfoUtil.getImei1());//imei1
mTvPhoneOtherImei.setText(GetSystemInfoUtil.getImei2());//imei2
mTvPhoneMeid.setText(GetSystemInfoUtil.getMeid());//meid
}
else if(Build.VERSION.SDK_INT>=23){
final TelephonyManager tm = (TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE);
String imei1 = tm.getDeviceId(0);
String imei2 = tm.getDeviceId(1);
mTvPhoneImei.setText(imei1);//imei1
mTvPhoneOtherImei.setText(imei2);//imei2
mTvPhoneMeid.setText(GetSystemInfoUtil.getMeid());//meid
}
/**
* 獲取SN
*
* @return
*/
public static String getSn(Context ctx) {
// if (android.os.Build.SERIAL.equals("unknown")) {
//MX3手機會出現這個
String serial = null;
try {
Class<?> c = Class.forName("android.os.SystemProperties");
Method get = c.getMethod("get", String.class);
serial = (String) get.invoke(c, "ro.serialno");
} catch (Exception ignored) {
}
return serial;
// } else {
// return android.os.Build.SERIAL;
// }
}
/**
* 系統4.0的時候
* 獲取手機IMEI 或者Meid
*
* @return 手機IMEI
*/
public static String getImeiOrMeid(Context ctx) {
TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Activity.TELEPHONY_SERVICE);
if (tm != null) {
return tm.getDeviceId();
}
return null;
}
/**
* 獲取IMEI IMEI2 MEID
* @param ctx
* @return
*/
@RequiresApi(api = Build.VERSION_CODES.M)
public static Map getImeiAndMeid(Context ctx) {
Map<String, String> map = new HashMap<String, String>();
TelephonyManager mTelephonyManager = (TelephonyManager) ctx.getSystemService(Activity.TELEPHONY_SERVICE);
Class<?> clazz = null;
Method method = null;//(int slotId)
try {
clazz = Class.forName("android.os.SystemProperties");
method = clazz.getMethod("get", String.class, String.class);
String gsm = (String) method.invoke(null, "ril.gsm.imei", "");
String meid = (String) method.invoke(null, "ril.cdma.meid", "");
map.put("meid", meid);
if (!TextUtils.isEmpty(gsm)) {
//the value of gsm like:xxxxxx,xxxxxx
String imeiArray[] = gsm.split(",");
if (imeiArray != null && imeiArray.length > 0) {
map.put("imei1", imeiArray[0]);
if (imeiArray.length > 1) {
map.put("imei2", imeiArray[1]);
} else {
map.put("imei2", mTelephonyManager.getDeviceId(1));
}
} else {
map.put("imei1", mTelephonyManager.getDeviceId(0));
map.put("imei2", mTelephonyManager.getDeviceId(1));
}
} else {
map.put("imei1", mTelephonyManager.getDeviceId(0));
map.put("imei2", mTelephonyManager.getDeviceId(1));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return map;
}
/**
* 獲取手機系統內部資訊工具類
* Created by yangbin on 3/2/2017.
*/
public class GetSystemInfoUtil {
/**
* 獲取當前手機系統版本號
*
* @return 系統版本號
*/
public static String getSystemVersion() {
return Build.DISPLAY;
//return android.os.Build.VERSION.RELEASE;
}
/**
* 獲取手機型號
*
* @return 手機型號
*/
public static String getSystemModel() {
return Build.MODEL;
}
/**
* 獲取手機廠商
*
* @return 手機廠商
*/
public static String getDeviceBrand() {
return Build.BRAND;
}
/**
* 獲取SN
*
* @return
*/
public static String getSn(Context ctx) {
String serial = null;
try {
Class<?> c = Class.forName("android.os.SystemProperties");
Method get = c.getMethod("get", String.class);
serial = (String) get.invoke(c, "ro.serialno");
} catch (Exception ignored) {
}
return serial;
}
/**
* 系統4.0的時候
* 獲取手機IMEI 或者Meid
*
* @return 手機IMEI
*/
public static String getImeiOrMeid(Context ctx) {
TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Activity.TELEPHONY_SERVICE);
if (tm != null) {
return tm.getDeviceId();
}
return null;
}
/**
* 拿到imei或者meid後判斷是有多少位數
*
* @param ctx
* @return
*/
public static int getNumber(Context ctx) {
int count = 0;
long number = Long.parseLong(getImeiOrMeid(ctx).trim());
while (number > 0) {
number = number / 10;
count++;
}
return count;
}
/**
* Flyme 說 5.0 6.0統一使用這個獲取IMEI IMEI2 MEID
* @param ctx
* @return
*/
@RequiresApi(api = Build.VERSION_CODES.M)
public static Map getImeiAndMeid(Context ctx) {
Map<String, String> map = new HashMap<String, String>();
TelephonyManager mTelephonyManager = (TelephonyManager) ctx.getSystemService(Activity.TELEPHONY_SERVICE);
Class<?> clazz = null;
Method method = null;//(int slotId)
try {
clazz = Class.forName("android.os.SystemProperties");
method = clazz.getMethod("get", String.class, String.class);
String gsm = (String) method.invoke(null, "ril.gsm.imei", "");
String meid = (String) method.invoke(null, "ril.cdma.meid", "");
map.put("meid", meid);
if (!TextUtils.isEmpty(gsm)) {
//the value of gsm like:xxxxxx,xxxxxx
String imeiArray[] = gsm.split(",");
if (imeiArray != null && imeiArray.length > 0) {
map.put("imei1", imeiArray[0]);
if (imeiArray.length > 1) {
map.put("imei2", imeiArray[1]);
} else {
map.put("imei2", mTelephonyManager.getDeviceId(1));
}
} else {
map.put("imei1", mTelephonyManager.getDeviceId(0));
map.put("imei2", mTelephonyManager.getDeviceId(1));
}
} else {
map.put("imei1", mTelephonyManager.getDeviceId(0));
map.put("imei2", mTelephonyManager.getDeviceId(1));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return map;
}
}
下面貼出我的整個工具類
/**
* 獲取手機系統內部資訊工具類
* Created by yangbin on 3/2/2017.
*/
public class GetSystemInfoUtil {
/**
* 獲取當前手機系統版本號
*
* @return 系統版本號
*/
public static String getSystemVersion() {
return Build.DISPLAY;
//return android.os.Build.VERSION.RELEASE;
}
/**
* 獲取手機型號
*
* @return 手機型號
*/
public static String getSystemModel() {
return Build.MODEL;
}
/**
* 獲取手機廠商
*
* @return 手機廠商
*/
public static String getDeviceBrand() {
return Build.BRAND;
}
/**
* 獲取SN
*
* @return
*/
public static String getSn(Context ctx) {
String serial = null;
try {
Class<?> c = Class.forName("android.os.SystemProperties");
Method get = c.getMethod("get", String.class);
serial = (String) get.invoke(c, "ro.serialno");
} catch (Exception ignored) {
}
return serial;
}
/**
* 系統4.0的時候
* 獲取手機IMEI 或者Meid
*
* @return 手機IMEI
*/
public static String getImeiOrMeid(Context ctx) {
TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Activity.TELEPHONY_SERVICE);
if (tm != null) {
return tm.getDeviceId();
}
return null;
}
/**
* 拿到imei或者meid後判斷是有多少位數
*
* @param ctx
* @return
*/
public static int getNumber(Context ctx) {
int count = 0;
long number = Long.parseLong(getImeiOrMeid(ctx).trim());
while (number > 0) {
number = number / 10;
count++;
}
return count;
}
/**
* Flyme 說 5.0 6.0統一使用這個獲取IMEI IMEI2 MEID
* @param ctx
* @return
*/
@RequiresApi(api = Build.VERSION_CODES.M)
public static Map getImeiAndMeid(Context ctx) {
Map<String, String> map = new HashMap<String, String>();
TelephonyManager mTelephonyManager = (TelephonyManager) ctx.getSystemService(Activity.TELEPHONY_SERVICE);
Class<?> clazz = null;
Method method = null;//(int slotId)
try {
clazz = Class.forName("android.os.SystemProperties");
method = clazz.getMethod("get", String.class, String.class);
String gsm = (String) method.invoke(null, "ril.gsm.imei", "");
String meid = (String) method.invoke(null, "ril.cdma.meid", "");
map.put("meid", meid);
if (!TextUtils.isEmpty(gsm)) {
//the value of gsm like:xxxxxx,xxxxxx
String imeiArray[] = gsm.split(",");
if (imeiArray != null && imeiArray.length > 0) {
map.put("imei1", imeiArray[0]);
if (imeiArray.length > 1) {
map.put("imei2", imeiArray[1]);
} else {
map.put("imei2", mTelephonyManager.getDeviceId(1));
}
} else {
map.put("imei1", mTelephonyManager.getDeviceId(0));
map.put("imei2", mTelephonyManager.getDeviceId(1));
}
} else {
map.put("imei1", mTelephonyManager.getDeviceId(0));
map.put("imei2", mTelephonyManager.getDeviceId(1));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return map;
}
}
Activity呼叫
if (Build.VERSION.SDK_INT < 21) {
//如果獲取系統的IMEI/MEID,14位代表meid 15位是imei
if (GetSystemInfoUtil.getNumber(getActivity()) == 14) {
mTvPhoneMeid.setText(GetSystemInfoUtil.getImeiOrMeid(getActivity()));//meid
} else if (GetSystemInfoUtil.getNumber(getActivity()) == 15) {
mTvPhoneImei.setText(GetSystemInfoUtil.getImeiOrMeid(getActivity()));//imei1
}
// 21版本是5.0,判斷是否是5.0以上的系統 5.0系統直接獲取IMEI1,IMEI2,MEID
} else if (Build.VERSION.SDK_INT >= 21) {
Map<String, String> map = GetSystemInfoUtil.getImeiAndMeid(getActivity());
mTvPhoneImei.setText(map.get("imei1"));//imei1
mTvPhoneOtherImei.setText(map.get("imei2"));//imei2
mTvPhoneMeid.setText(map.get("meid"));//meid
}
mTvPhoneSn.setText(GetSystemInfoUtil.getSn(getActivity()));//SN
mTvPhoneModels.setText(GetSystemInfoUtil.getSystemModel());//手機型號 PRO6
mTvVersionNumber.setText(GetSystemInfoUtil.getSystemVersion());//軟體版本號 FLYME 6.02
}
相關文章
- 手機更卡了!安卓5.0停止預設資料加密安卓加密
- android雙待手機獲取每一張SIM卡的imeiAndroid
- 蘋果出“雙卡雙待”手機,聽說侵權“雙卡雙待”的鼻祖酷派?蘋果
- 讓iPhone也能雙卡雙待雙系統 機甲評測iPhone
- 微軟宣佈Win10你的手機將新增支援安卓雙SIM卡功能微軟Win10安卓
- 中高階雙卡雙待手機精選 一機雙號更實用
- iOS獲取SIM卡資訊iOS
- 魅族手機將獲安卓5.0更新 全新Flyme下月推送安卓
- Airphone手機殼幫你把iPhone變成雙卡雙待AIiPhone
- Windows mobile 下讀取手機SIM卡資訊Windows
- 手機卡刷刷機通用教程
- “安卓機皇”華為Mate20 Pro斬獲手機界“奧斯卡”意味著什麼安卓
- 手機SD卡修復SD卡
- 適用mini SD卡的手機SD卡
- Android Media(多媒體)獲取手機記憶體卡下的音樂並播放Android記憶體
- 獲取SD卡上所有的圖片SD卡
- Windows Phone 7 SIM卡資訊獲取Windows
- iPhone Xs Max支援雙卡雙待嗎?iPhone Xs支援雙卡雙待嗎iPhone
- 秒開快取系統支援的硬體陣列卡快取陣列
- 安卓6.0+十核 樂視超級手機2獲入網許可安卓
- 堅果手機怎麼裝卡 堅果手機 sim卡安裝圖文教程
- 獲取SD卡序列號和廠商IDSD卡
- 高效獲取銀行卡髮卡行所在地資訊——利用銀行卡歸屬地查詢介面
- 市售雙卡熱門手機盤點 工作生活兩不耽誤
- iQOO手機支援5G網路嗎?iQOO手機支援雙電信卡使用
- 360奇酷手機怎麼裝卡 360奇酷手機 sim卡安裝圖文教程
- 紅米手機3 sim卡安裝教程 紅米3怎麼裝卡
- 雙卡雙待/超廉價機 微軟Lumia 430釋出微軟
- 物聯網路卡和手機卡的區別
- sim卡及手機的密碼詳解密碼
- 明年的iPhone支援雙卡雙待 網速更快iPhone
- android 從SIM卡獲取聯絡人資訊Android
- android系統中獲取imei號和其他手機資訊Android
- 雙卡雙4G智慧機盤點 居家旅行必備利器
- 國產手機將不能再用安卓系統了?安卓
- 微信防疫行程卡怎麼領取?微信防疫行程卡領取方法行程
- 企業髮卡網原始碼_知宇髮卡系統原始碼原始碼
- 輕燃卡卡新零售系統開發原始碼搭建原始碼