Android獲取IMEI和MEID

IMEI(International Mobile Equipment Identity)是國(guó)際移動(dòng)設(shè)備身份碼的縮寫,由15位數(shù)字組成的電子串號(hào)與每臺(tái)手機(jī)一一對(duì)應(yīng),且該碼全世界唯一。
MEID(Mobile Equipment Identifier)移動(dòng)設(shè)備識(shí)別碼,是CDMA手機(jī)的身份識(shí)別碼,也是每臺(tái)CDMA手機(jī)或通訊平板唯一的識(shí)別碼,由14位數(shù)字組成。

在破解微信數(shù)據(jù)庫(kù)時(shí),需要獲取手機(jī)的DeviceId,但是有時(shí)會(huì)出現(xiàn)打不開的情況,報(bào)出file is not a database: , while compiling: select count(*) from sqlite_master的異常,這時(shí)發(fā)現(xiàn)我的數(shù)據(jù)庫(kù)密碼和之前的不一致,對(duì)比一下發(fā)現(xiàn)獲取的deviceId不一致導(dǎo)致的,難道手機(jī)的deviceId也會(huì)變來(lái)變?nèi)幔?br> 搜了一下資料,發(fā)現(xiàn)獲取手機(jī)的deviceId還真沒(méi)想的那么容易。一般情況我們獲取手機(jī)的DeviceId也就是手機(jī)的IMEI碼,一般通過(guò)如下代碼。此外還需要獲取READ_PHONE_STATE權(quán)限。

private String getPhoneIMEI() {
    TelephonyManager tm = (TelephonyManager) getContext().getSystemService(Service.TELEPHONY_SERVICE);
    return tm.getDeviceId();
}

一個(gè)雙卡手機(jī)不止一個(gè)IMEI值,全網(wǎng)通雙卡手機(jī)有兩個(gè)IMEI和一個(gè)MEID,Android6.0的API中提供了這樣的方法getDeviceId(int slotIndex)

type value meaning
int PHONE_TYPE_CDMA Phone radio is CDMA
int PHONE_TYPE_GSM Phone radio is GSM
private String getPhoneIMEI(int slotIndex) {
    TelephonyManager tm = (TelephonyManager) getContext().getSystemService(Service.TELEPHONY_SERVICE);
    return tm.getIMEI(slotIndex);
}

private String getPhoneMEID(int slotIndex) {
    TelephonyManager tm = (TelephonyManager) getContext().getSystemService(Service.TELEPHONY_SERVICE);
    return tm.getMEID(slotIndex);
}

在Android5.0系統(tǒng)中,可以通過(guò)反射獲取IMEI和MEID的值。

private String getIMEI(int slotId){
        try {
            Class clazz = Class.forName("android.os.SystemProperties");
            Method method = clazz.getMethod("get", String.class, String.class);
            String imei = (String) method.invoke(null, "ril.gsm.imei", "");
            if(!TextUtils.isEmpty(imei)){
                String[] split = imei.split(",");
                if(split.length > slotId){
                    imei = split[slotId];
                }
                Log.d(TAG,"getIMEI imei: "+ imei);
                return imei;
            }
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | ClassNotFoundException e) {
            e.printStackTrace();
            Log.w(TAG,"getIMEI error : "+ e.getMessage());
        }
        return "";
    }
    
private String getMEID(){
        try {
            Class clazz = Class.forName("android.os.SystemProperties");
            Method method = clazz.getMethod("get", String.class, String.class);

            String meid = (String) method.invoke(null, "ril.cdma.meid", "");
            if(!TextUtils.isEmpty(meid)){
                Log.d(TAG,"getMEID meid: "+ meid);
                return meid;
            }
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | ClassNotFoundException e) {
            e.printStackTrace();
            Log.w(TAG,"getMEID error : "+ e.getMessage());
        }
        return "";
    }

至于上面提到的手機(jī)deviceId改變因?yàn)橹罢{(diào)用tm.getDeviceId()返回的imei值,后來(lái)返回meid值導(dǎo)致的,什么原因?qū)е碌倪€沒(méi)發(fā)現(xiàn)。

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

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

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