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


当前回答

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-->

其他回答

对于那些寻找Kotlin版本的人,您可以使用这样的东西;

private fun telephonyService() {
    val telephonyManager = getSystemService(TELEPHONY_SERVICE) as TelephonyManager
    val imei = if (android.os.Build.VERSION.SDK_INT >= 26) {
        Timber.i("Phone >= 26 IMEI")
        telephonyManager.imei
    } else {
        Timber.i("Phone IMEI < 26")
        telephonyManager.deviceId
    }

    Timber.i("Phone IMEI $imei")
}

注意:您必须使用checkSelfPermission或任何您使用的方法将上面的telephonyService()与权限检查一起包装。

还可以在清单文件中添加此权限;

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

TelephonyManager的getDeviceId()方法返回唯一的设备ID,例如GSM电话的IMEI, CDMA电话的MEID或ESN。如果设备ID不可用,则返回null。

Java代码

package com.AndroidTelephonyManager;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.TextView;

public class AndroidTelephonyManager extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TextView textDeviceID = (TextView)findViewById(R.id.deviceid);

    //retrieve a reference to an instance of TelephonyManager
    TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

    textDeviceID.setText(getDeviceID(telephonyManager));

}

String getDeviceID(TelephonyManager phonyManager){

 String id = phonyManager.getDeviceId();
 if (id == null){
  id = "not available";
 }

 int phoneType = phonyManager.getPhoneType();
 switch(phoneType){
 case TelephonyManager.PHONE_TYPE_NONE:
  return "NONE: " + id;

 case TelephonyManager.PHONE_TYPE_GSM:
  return "GSM: IMEI=" + id;

 case TelephonyManager.PHONE_TYPE_CDMA:
  return "CDMA: MEID/ESN=" + id;

 /*
  *  for API Level 11 or above
  *  case TelephonyManager.PHONE_TYPE_SIP:
  *   return "SIP";
  */

 default:
  return "UNKNOWN: ID=" + id;
 }

}
}

XML

<linearlayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
<textview android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/hello">
<textview android:id="@+id/deviceid" android:layout_height="wrap_content" android:layout_width="fill_parent">
</textview></textview></linearlayout> 

许可要求 manifest文件中的READ_PHONE_STATE。

新更新:

对于Android版本6及以上,WLAN MAC地址已弃用,请遵循Trevor Johns的回答

更新:

对于设备的唯一标识,可以使用Secure.ANDROID_ID。

旧的回答:

使用IMEI作为唯一设备ID的缺点:

IMEI依赖于设备的Simcard插槽,所以它不是 对于不使用Simcard的设备,可以获得IMEI。 在双sim卡设备中,我们为同一个设备获得2个不同的imei,因为它有2个sim卡插槽。

您可以使用WLAN MAC地址字符串(不推荐用于Marshmallow和Marshmallow+,因为WLAN MAC地址已在Marshmallow转发上被弃用。所以你会得到一个伪值)

我们也可以使用WLAN MAC地址获得android手机的唯一ID。MAC地址对于所有设备都是唯一的,它适用于所有类型的设备。

使用WLAN MAC地址作为设备ID的优点:

它是所有类型设备的唯一标识符(智能手机和 平板电脑)。 如果重新安装应用程序,它将保持唯一

使用WLAN MAC地址作为设备ID的缺点:

给你一个虚假的价值从棉花糖和以上。 如果设备没有wifi硬件,MAC地址为空, 但通常可以看到大多数Android设备都有wifi 硬件和市场上几乎没有没有wifi的设备 硬件。

来源:technetexperts.com

试试这个(总是需要第一个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();
        }

对不可重置设备标识符的限制

从Android 10开始,应用程序必须拥有READ_PRIVILEGED_PHONE_STATE特权权限才能访问设备的不可重置标识符,包括IMEI和序列号。