为了唯一地识别每个设备,我想使用IMEI(或CDMA设备的ESN号)。如何以编程方式访问它?
当前回答
你需要在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;
}
其他回答
来自: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;
}
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-->
API等级11或以上:
case TelephonyManager.PHONE_TYPE_SIP:
return "SIP";
TelephonyManager tm= (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
textDeviceID.setText(getDeviceID(tm));
或者你可以使用ANDROID_ID设置从Android.Provider.Settings.System(这里描述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是显示在我们的日志中的值)
推荐文章
- Android Studio, logcat在应用程序关闭后清理
- 在android中从上下文获取活动
- 无法解析主机"<URL here>"没有与主机名关联的地址
- getActivity()在Fragment函数中返回null
- 按钮背景是透明的
- 在Mac OS X上哪里安装Android SDK ?
- 我如何获得图像缩放功能?
- 在Android应用程序中显示当前时间和日期
- BottomSheetDialogFragment的圆角
- 在应用程序启动时出现“无法获得BatchedBridge,请确保您的bundle被正确打包”的错误
- 我如何改变默认对话框按钮的文本颜色在安卓5
- 更改单选按钮的圆圈颜色
- 如何在android中复制一个文件?
- adb找不到我的设备/手机(MacOS X)
- 如何在新的材质主题中改变背面箭头的颜色?