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


当前回答

我使用以下代码获取IMEI或使用Secure。ANDROID_ID作为替代,当设备没有电话功能时:

/**
 * Returns the unique identifier for the device
 *
 * @return unique identifier for the device
 */
public String getDeviceIMEI() {
    String deviceUniqueIdentifier = null;
    TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
    if (null != tm) {
        deviceUniqueIdentifier = tm.getDeviceId();
    }
    if (null == deviceUniqueIdentifier || 0 == deviceUniqueIdentifier.length()) {
        deviceUniqueIdentifier = Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID);
    }
    return deviceUniqueIdentifier;
}

其他回答

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.telephony.TelephonyManager.getDeviceId()。

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

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

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

为了做到这一点。

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

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

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

对于那些寻找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;
}

你可以使用这个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。