为了唯一地识别每个设备,我想使用IMEI(或CDMA设备的ESN号)。如何以编程方式访问它?


当前回答

获取IMEI(国际移动设备标识符)

public String getIMEI(Activity activity) {
    TelephonyManager telephonyManager = (TelephonyManager) activity
            .getSystemService(Context.TELEPHONY_SERVICE);
    return telephonyManager.getDeviceId();
}

获取设备唯一id

public String getDeviceUniqueID(Activity activity){
    String device_unique_id = Secure.getString(activity.getContentResolver(),
            Secure.ANDROID_ID);
    return device_unique_id;
}

其他回答

你需要调用android.telephony.TelephonyManager.getDeviceId()。

这将返回唯一标识该设备的字符串(GSM上的IMEI, CDMA上的MEID)。

你需要在AndroidManifest.xml中设置以下权限:

< uses-permission android: name = " android.permission。READ_PHONE_STATE " / >

为了做到这一点。

话虽如此,做这件事要小心。用户不仅会想知道为什么您的应用程序要访问他们的电话堆栈,而且如果用户获得了新设备,可能很难迁移数据。

更新:正如下面评论中提到的,这不是一种安全的认证用户的方式,并引起了隐私问题。不建议使用。相反,如果您想实现一个无障碍的登录系统,请查看谷歌+ Login API。

如果你只是想在用户重置手机(或购买新设备)时以一种轻量级的方式持久化一束字符串,Android Backup API也是可用的。

API等级11或以上:

case TelephonyManager.PHONE_TYPE_SIP: 
return "SIP";

TelephonyManager tm= (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
textDeviceID.setText(getDeviceID(tm));

你需要在AndroidManifest.xml中设置以下权限:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

要获得IMEI(国际移动设备标识符),如果它高于API级别26,那么我们将telephonyManager.getImei()作为null,因此,我们使用ANDROID_ID作为唯一标识符。

 public static String getIMEINumber(@NonNull final Context context)
            throws SecurityException, NullPointerException {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String imei;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            assert tm != null;
            imei = tm.getImei();
            //this change is for Android 10 as per security concern it will not provide the imei number.
            if (imei == null) {
                imei = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
            }
        } else {
            assert tm != null;
            if (tm.getDeviceId() != null && !tm.getDeviceId().equals("000000000000000")) {
                imei = tm.getDeviceId();
            } else {
                imei = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
            }
        }

        return imei;
    }

Kotlin代码获取DeviceId (IMEI)处理权限和可比性检查的所有android版本:

 val  telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)
        == PackageManager.PERMISSION_GRANTED) {
        // Permission is  granted
        val imei : String? = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)  telephonyManager.imei
        // older OS  versions
        else  telephonyManager.deviceId

        imei?.let {
            Log.i("Log", "DeviceId=$it" )
        }

    } else {  // Permission is not granted

    }

将此权限添加到AndroidManifest.xml:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/> <!-- IMEI-->

试试这个(总是需要第一个IMEI)

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();
        }