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


当前回答

你可以使用这个TelephonyManager TELEPHONY_SERVICE函数来获取唯一的设备ID, 要求权限:READ_PHONE_STATE

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

例如,GSM手机的IMEI, CDMA手机的MEID或ESN。

/**
 * Gets the device unique id called IMEI. Sometimes, this returns 00000000000000000 for the
 * rooted devices.
 **/
public static String getDeviceImei(Context ctx) {
    TelephonyManager telephonyManager = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
    return telephonyManager.getDeviceId();
}

如果设备ID不可用,则返回null。

其他回答

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

从Android 10开始,应用程序必须拥有READ_PRIVILEGED_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"/>

你无法从andorid10 +或29+手机中获得imei号码,这里是用于为设备创建imei号码的替代功能。

public  static String getDeviceID(){
    String devIDShort = "35" + //we make this look like a valid IMEI
            Build.BOARD.length()%10+ Build.BRAND.length()%10 +
            Build.CPU_ABI.length()%10 + Build.DEVICE.length()%10 +
            Build.DISPLAY.length()%10 + Build.HOST.length()%10 +
            Build.ID.length()%10 + Build.MANUFACTURER.length()%10 +
            Build.MODEL.length()%10 + Build.PRODUCT.length()%10 +
            Build.TAGS.length()%10 + Build.TYPE.length()%10 +
            Build.USER.length()%10 ; //13 digits

    return  devIDShort;
}

来自:http://mytechead.wordpress.com/2011/08/28/how-to-get-imei-number-of-android-device/:

下面的代码可以帮助获取android设备的IMEI号码:

TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String device_id = tm.getDeviceId();

Android Manifest中需要的权限:

android.permission.READ_PHONE_STATE

注意:如平板电脑或不能作为移动电话的设备 IMEI将为空。

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。