368

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

4

22 回答 22

408

你想打电话android.telephony.TelephonyManager.getDeviceId()

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

您需要以下权限AndroidManifest.xml

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

为此。

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

更新:正如下面评论中提到的,这不是验证用户的安全方式,并且会引起隐私问题。不推荐。相反,如果您想实现无摩擦登录系统,请查看Google+ 登录 API 。

如果您只想在用户重置手机(或购买新设备)时使用一种轻量级的方式来持久保存字符串包,也可以使用Android备份 API 。

于 2009-12-29T01:01:10.530 回答
289

除了 Trevor Johns 的回答之外,您还可以按如下方式使用它:

TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.getDeviceId();

您应该将以下权限添加到您的 Manifest.xml 文件中:

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

在模拟器中,您可能会得到一个类似“00000 ...”的值。如果设备 ID 不可用,getDeviceId() 将返回 NULL。

于 2010-06-09T19:44:59.890 回答
64

当设备没有电话功能时,我使用以下代码获取 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;
}
于 2013-04-15T12:58:54.597 回答
41

或者,您可以使用 Android.Provider.Settings.System 中的 ANDROID_ID 设置(如此处strazerre.com所述)。

这样做的好处是它不需要特殊权限,但如果另一个应用程序具有写访问权限并对其进行更改,则可以更改(这显然不寻常但并非不可能)。

仅供参考,这里是博客中的代码:

import android.provider.Settings;
import android.provider.Settings.System;   

String androidID = System.getString(this.getContentResolver(),Secure.ANDROID_ID);

实施说明如果 ID 对系统架构至关重要,您需要注意,在实践中发现一些非常低端的 Android 手机和平板电脑重复使用相同的 ANDROID_ID(9774d56d682e549c 是我们日志中显示的值)

于 2010-10-20T10:22:15.963 回答
35

来自: 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 将为空。

于 2012-04-28T17:32:30.153 回答
23

获取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;
}
于 2014-09-24T04:17:56.607 回答
20

对于 Android 6.0+,游戏已经改变,所以我建议你使用它;

最好的方法是在运行时,否则你会得到权限错误。

   /**
 * A loading screen after AppIntroActivity.
 */
public class LoadingActivity extends BaseActivity {
private static final int MY_PERMISSIONS_REQUEST_READ_PHONE_STATE = 0;
private TextView loading_tv2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_loading);

    //trigger 'loadIMEI'
    loadIMEI();
    /** Fading Transition Effect */
    overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}

/**
 * Called when the 'loadIMEI' function is triggered.
 */
public void loadIMEI() {
    // Check if the READ_PHONE_STATE permission is already available.
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)
            != PackageManager.PERMISSION_GRANTED) {
        // READ_PHONE_STATE permission has not been granted.
        requestReadPhoneStatePermission();
    } else {
        // READ_PHONE_STATE permission is already been granted.
        doPermissionGrantedStuffs();
    }
}



/**
 * Requests the READ_PHONE_STATE permission.
 * If the permission has been denied previously, a dialog will prompt the user to grant the
 * permission, otherwise it is requested directly.
 */
private void requestReadPhoneStatePermission() {
    if (ActivityCompat.shouldShowRequestPermissionRationale(this,
            Manifest.permission.READ_PHONE_STATE)) {
        // Provide an additional rationale to the user if the permission was not granted
        // and the user would benefit from additional context for the use of the permission.
        // For example if the user has previously denied the permission.
        new AlertDialog.Builder(LoadingActivity.this)
                .setTitle("Permission Request")
                .setMessage(getString(R.string.permission_read_phone_state_rationale))
                .setCancelable(false)
                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        //re-request
                        ActivityCompat.requestPermissions(LoadingActivity.this,
                                new String[]{Manifest.permission.READ_PHONE_STATE},
                                MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
                    }
                })
                .setIcon(R.drawable.onlinlinew_warning_sign)
                .show();
    } else {
        // READ_PHONE_STATE permission has not been granted yet. Request it directly.
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE},
                MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
    }
}

/**
 * Callback received when a permissions request has been completed.
 */
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {

    if (requestCode == MY_PERMISSIONS_REQUEST_READ_PHONE_STATE) {
        // Received permission result for READ_PHONE_STATE permission.est.");
        // Check if the only required permission has been granted
        if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // READ_PHONE_STATE permission has been granted, proceed with displaying IMEI Number
            //alertAlert(getString(R.string.permision_available_read_phone_state));
            doPermissionGrantedStuffs();
        } else {
            alertAlert(getString(R.string.permissions_not_granted_read_phone_state));
          }
    }
}

private void alertAlert(String msg) {
    new AlertDialog.Builder(LoadingActivity.this)
            .setTitle("Permission Request")
            .setMessage(msg)
            .setCancelable(false)
            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // do somthing here
                }
            })
            .setIcon(R.drawable.onlinlinew_warning_sign)
            .show();
}


public void doPermissionGrantedStuffs() {
    //Have an  object of TelephonyManager
    TelephonyManager tm =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    //Get IMEI Number of Phone  //////////////// for this example i only need the IMEI
    String IMEINumber=tm.getDeviceId();

    /************************************************
     * **********************************************
     * This is just an icing on the cake
     * the following are other children of TELEPHONY_SERVICE
     *
     //Get Subscriber ID
     String subscriberID=tm.getDeviceId();

     //Get SIM Serial Number
     String SIMSerialNumber=tm.getSimSerialNumber();

     //Get Network Country ISO Code
     String networkCountryISO=tm.getNetworkCountryIso();

     //Get SIM Country ISO Code
     String SIMCountryISO=tm.getSimCountryIso();

     //Get the device software version
     String softwareVersion=tm.getDeviceSoftwareVersion()

     //Get the Voice mail number
     String voiceMailNumber=tm.getVoiceMailNumber();


     //Get the Phone Type CDMA/GSM/NONE
     int phoneType=tm.getPhoneType();

     switch (phoneType)
     {
     case (TelephonyManager.PHONE_TYPE_CDMA):
     // your code
     break;
     case (TelephonyManager.PHONE_TYPE_GSM)
     // your code
     break;
     case (TelephonyManager.PHONE_TYPE_NONE):
     // your code
     break;
     }

     //Find whether the Phone is in Roaming, returns true if in roaming
     boolean isRoaming=tm.isNetworkRoaming();
     if(isRoaming)
     phoneDetails+="\nIs In Roaming : "+"YES";
     else
     phoneDetails+="\nIs In Roaming : "+"NO";


     //Get the SIM state
     int SIMState=tm.getSimState();
     switch(SIMState)
     {
     case TelephonyManager.SIM_STATE_ABSENT :
     // your code
     break;
     case TelephonyManager.SIM_STATE_NETWORK_LOCKED :
     // your code
     break;
     case TelephonyManager.SIM_STATE_PIN_REQUIRED :
     // your code
     break;
     case TelephonyManager.SIM_STATE_PUK_REQUIRED :
     // your code
     break;
     case TelephonyManager.SIM_STATE_READY :
     // your code
     break;
     case TelephonyManager.SIM_STATE_UNKNOWN :
     // your code
     break;

     }
     */
    // Now read the desired content to a textview.
    loading_tv2 = (TextView) findViewById(R.id.loading_tv2);
    loading_tv2.setText(IMEINumber);
}
}

希望这对您或某人有所帮助。

于 2016-05-02T02:15:19.573 回答
18

由于 API 26getDeviceId()已折旧,因此您可以使用以下代码来满足 API 26 和更早版本的需求

TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String imei="";
if (android.os.Build.VERSION.SDK_INT >= 26) {
  imei=telephonyManager.getImei();
}
else
{
  imei=telephonyManager.getDeviceId();
}

不要忘记添加READ_PHONE_STATE使用上述代码的权限请求。

更新:从 Android 10 开始,用户应用程序只能获取不可重置的硬件标识符,如 IMEI。

于 2017-08-17T11:10:38.607 回答
17

新更新:

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

更新:

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

老答案:

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

  • IMEI 依赖于设备的 Simcard 插槽,因此无法获取不使用 Simcard 的设备的 IMEI。在双卡设备中,我们为同一设备获得 2 个不同的 IMEI,因为它有 2 个 SIM 卡插槽。

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

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

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

  • 它是所有类型设备(智能手机和平板电脑)的唯一标识符。

  • 如果重新安装应用程序,它仍然是唯一的

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

  • 给你一个棉花糖及以上的虚假价值。

  • 如果设备没有 wifi 硬件,那么你会得到空 MAC 地址,但通常可以看到大多数 Android 设备都有 wifi 硬件,市场上几乎没有没有 wifi 硬件的设备。

资料来源:technetexperts.com

于 2016-07-07T16:31:28.837 回答
11

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> 

清单文件中需要 READ_PHONE_STATE权限。

于 2013-06-17T07:20:54.353 回答
5

试试这个(需要始终获得第一个 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();
        }
于 2019-03-01T05:06:12.907 回答
5

您可以使用此 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

于 2017-09-04T08:19:52.377 回答
3

使用以下代码为您提供 IMEI 号码:

TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
System.out.println("IMEI::" + telephonyManager.getDeviceId());
于 2013-12-17T07:00:19.390 回答
3

该方法getDeviceId()已弃用。有一种新方法getImei(int)

在这里检查

于 2018-06-28T13:41:59.327 回答
2

您需要在 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;
    }
于 2020-01-16T14:13:08.067 回答
1

API 11 级或以上:

case TelephonyManager.PHONE_TYPE_SIP: 
return "SIP";

TelephonyManager tm= (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
textDeviceID.setText(getDeviceID(tm));
于 2013-05-24T12:39:07.147 回答
1

用于获取 DeviceId (IMEI) 的 Kotlin 代码,并针对所有 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-->
于 2018-12-23T12:06:01.067 回答
1

对于 Android 10 以下代码对我有用。

val uid: String = Settings.Secure.getString(ctx.applicationContext.contentResolver, Settings.Secure.ANDROID_ID)
if (ContextCompat.checkSelfPermission(ctx, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
            imei = when {
                Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q -> {
                    uid
                }
                Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> {
                    telephonyManager.imei
                }
                else -> {
                    telephonyManager.deviceId
                }
            }
        }

于 2020-05-26T16:11:47.213 回答
1

使用下面的代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        String[] permissions = {Manifest.permission.READ_PHONE_STATE};
        if (ActivityCompat.checkSelfPermission(this,
                Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(permissions, READ_PHONE_STATE);
        }
    } else {
        try {
            TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                return;
            }
            String imei = telephonyManager.getDeviceId();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

并调用以下代码的 onRequestPermissionsResult 方法:

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
        case READ_PHONE_STATE:
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                try {
                    TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
                    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                        return;
                    }
                    String imei = telephonyManager.getDeviceId();

                } catch (Exception e) {
                    e.printStackTrace();
                }
    }
}

在您的 AndroidManifest.xml 中添加以下权限:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
于 2020-02-11T10:19:30.240 回答
0

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

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

于 2021-08-01T16:21:38.347 回答
0

对于那些寻找 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"/>
于 2018-12-02T10:29:18.977 回答
0

您无法从 andorid 10+ 或 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;
}
于 2021-07-13T07:19:41.877 回答