How to get the device's IMEI/ESN programmatically in android?

后端 未结 20 2542
滥情空心
滥情空心 2020-11-22 05:19

To identify each devices uniquely I would like to use the IMEI (or ESN number for CDMA devices). How to access this programmatically?

20条回答
  •  Happy的楠姐
    2020-11-22 05:49

    Try this(need to get first IMEI always)

    TelephonyManager mTelephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
            if (ActivityCompat.checkSelfPermission(LoginActivity.this,Manifest.permission.READ_PHONE_STATE)!= PackageManager.PERMISSION_GRANTED) {
    
             return;
    }
    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    if (mTelephony.getPhoneCount() == 2) {
                        IME = mTelephony.getImei(0);
                    }else{
                        IME = mTelephony.getImei();
                    }
                }else{
                    if (mTelephony.getPhoneCount() == 2) {
                        IME = mTelephony.getDeviceId(0);
                    } else {
                        IME = mTelephony.getDeviceId();
                    }
                }
            } else {
                IME = mTelephony.getDeviceId();
            }
    

提交回复
热议问题