小智 17
我迟到了发布答案。我仍然相信我的回答会对某人有所帮助。Android 10 限制开发者访问 IMEI 号码。
您可以通过获取软件 ID 来获得替代解决方案。您可以使用软件 ID 作为唯一 ID。请找到我在应用程序中使用的以下代码。
public static String getDeviceId(Context context) {
String deviceId;
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
deviceId = Settings.Secure.getString(
context.getContentResolver(),
Settings.Secure.ANDROID_ID);
} else {
final TelephonyManager mTelephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if (mTelephony.getDeviceId() != null) {
deviceId = mTelephony.getDeviceId();
} else {
deviceId = Settings.Secure.getString(
context.getContentResolver(),
Settings.Secure.ANDROID_ID);
}
}
return deviceId;
}
Run Code Online (Sandbox Code Playgroud)
小智 14
这在 Android Q 中不起作用。第三方应用程序不能使用 IMEI,也不能使用手机的序列号和其他不可重置的设备标识符。
能够使用这些权限的唯一权限是 READ_PRIVILEGED_PHONE_STATE,并且不能被任何第三方应用程序使用 - 制造和软件应用程序。如果您使用该方法,您将收到错误安全异常或得到 null 。
您仍然可以尝试使用以下方法获取唯一 ID:
import android.provider.Settings.Secure;
private String android_id = Secure.getString(getContext().getContentResolver(),Secure.ANDROID_ID);
Run Code Online (Sandbox Code Playgroud)
tak*_*rsh 12
Android Q已限制访问IMEI和序列号。它仅适用于具有特殊运营商许可的平台和应用。此外,权限READ_PRIVILEGED_PHONE_STATE不适用于非平台应用程序。
如果您尝试访问它,则抛出以下异常
java.lang.SecurityException: getImeiForSlot: The user 10180 does not meet the requirements to access device identifiers.
Run Code Online (Sandbox Code Playgroud)
请参阅文档:https : //developer.android.com/preview/privacy/data-identifiers#device-ids
正如最佳实践所建议的那样。“您可以避免使用硬件标识符,例如 SSAID (Android ID) 和 IMEI,而不限制所需的功能。”
而是使用实例 ID,例如String uniqueID = UUID.randomUUID().toString();或FirebaseInstanceId.getInstance().getId();
小智 7
不确定 IMEI 号码,但您可以通过这种方式获取 simSerialNumber 和其他运营商信息。
getSimSerialNumber() 需要 Android 10 以后的特权权限,第三方应用无法注册此权限。
请参阅:https : //developer.android.com/about/versions/10/privacy/changes#non-resettable-device-ids
一种可能的解决方案是使用 Android 5.1 中的 TELEPHONY_SUBSCRIPTION_SERVICE 来检索 SIM 序列号。以下步骤:
从订阅对象中检索 sim 详细信息。
if ( isPermissionGranted(READ_PHONE_STATE) ) {
String simSerialNo="";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
SubscriptionManager subsManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
List<SubscriptionInfo> subsList = subsManager.getActiveSubscriptionInfoList();
if (subsList!=null) {
for (SubscriptionInfo subsInfo : subsList) {
if (subsInfo != null) {
simSerialNo = subsInfo.getIccId();
}
}
}
} else {
TelephonyManager tMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
simSerialNo = tMgr.getSimSerialNumber();
}
}
Run Code Online (Sandbox Code Playgroud)检查这是否有帮助
小智 7
获得IMEI号码的最佳方式如下:
public static String getIMEIDeviceId(Context context) {
String deviceId;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
{
deviceId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
} else {
final TelephonyManager mTelephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (context.checkSelfPermission(Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
return "";
}
}
assert mTelephony != null;
if (mTelephony.getDeviceId() != null)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
deviceId = mTelephony.getImei();
}else {
deviceId = mTelephony.getDeviceId();
}
} else {
deviceId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
}
}
Log.d("deviceId", deviceId);
return deviceId;
}
Run Code Online (Sandbox Code Playgroud)
只需复制方法并使用它。一定会的。但是,您可能知道在 android Q(版本 10)中无法获取 IMEI。在此代码中,您可以通过任何设备或任何 API 级别获取唯一标识符(替代 ID)。它 100% 有效,谢谢!!并享受编码:)
小智 6
如果您的应用面向 Android 10 或更高版本,则会发生 SecurityException。
以下模块受到影响...
Build
getSerial()
TelephonyManager
getImei()
getDeviceId()
getMeid()
getSimSerialNumber()
getSubscriberId()
Run Code Online (Sandbox Code Playgroud)
所以你无法获得 android 10 的 IMEI 号,你必须使用另一个唯一标识符,例如 Android ID
它是设备唯一的 64 位十六进制编号
private String android_id =
Secure.getString(getContext().getContentResolver(), Secure.ANDROID_ID);
Run Code Online (Sandbox Code Playgroud)
您可以更改其他方式,我使用 uuid 替换设备 ID。
String uniquePseudoID = "35" +
Build.BOARD.length() % 10 +
Build.BRAND.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;
String serial = Build.getRadioVersion();
String uuid = new UUID(uniquePseudoID.hashCode(), serial.hashCode()).toString();
AppLog.d("Device ID",uuid);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10277 次 |
| 最近记录: |