如何在android中以编程方式获取设备的IMEI / ESN?

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

你想调用android.telephony.TelephonyManager.getDeviceId()

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

您需要在AndroidManifest.xml拥有以下权限:

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

为此。

话虽如此,小心这样做。 用户不仅会怀疑为什么你的应用程序正在访问他们的电话栈,如果用户获得一个新的设备,可能很难迁移数据。

更新:如下面的评论所述,这不是一个安全的方式来validation用户,并引发隐私问题。 这是不build议的。 相反,如果您想实施无摩擦login系统,请查看Google+loginAPI 。

如果您只想要一个轻量级的方式来持续一串string,以便用户重置手机(或购买新设备)时, Android备份API也可用。

除了特雷弗·约翰斯的回答,你可以使用这个如下:

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

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

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

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

当设备没有电话function时,我使用以下代码来获取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; } 

或者您可以使用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是我们的日志中显示的值)

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

获得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版本6及以上,WLAN MAC地址已被弃用

更新:

为了唯一标识设备,您可以使用Secure.ANDROID_ID 。

老答案:

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

  • IMEI取决于设备的SIM卡插槽,因此无法获取不使用SIM卡的设备的IMEI。 在双SIM设备中,我们针对相同的设备获得2个不同的IMEI,因为它具有2个SIM卡插槽。

您可以使用无线局域网MAC地址string(不推荐用于棉花糖和棉花糖+因为无线局域网MAC地址已被弃用在棉花糖前,所以你会得到一个假的价值)

我们也可以使用WLAN MAC地址获得Android手机的唯一ID。 MAC地址对于所有设备是唯一的,并且适用于各种设备。

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

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

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

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

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

  • 如果设备没有wifi硬件,那么你会得到空MAC地址,但一般来说,大多数的Android设备都有wifi硬件,市面上没有wifi硬件的设备也很less。

消息来源: technetexperts.com

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

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

  /** * 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); } } 

希望这可以帮助你或某人。

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。

在API 26中getDeviceId()被折旧,所以你可以使用下面的代码来应对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”的权限请求以使用上面的代码。

使用下面的代码给你imi号码

 TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); System.out.println("Emi::" + telephonyManager.getDeviceId()); 

对于API Level 11或更高级别的情况TelephonyManager.PHONE_TYPE_SIP:返回“SIP”;

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

您可以使用此TelephonyManager TELEPHONY_SERVICEfunction获取唯一的设备ID ,需要权限:READ_PHONE_STATE

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

例如, 用于GSMIMEI用于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