我想获取Android设备的唯一标识符。我已经尝试使用以下代码
String ts = Context.TELEPHONY_SERVICE;
TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(ts);
但是我知道这只适用于手机。
那么如果我的应用程序在笔记本电脑、小型笔记本电脑或其他类型的设备上运行怎么办?在这种情况下,如何获取唯一标识符?
我想获取Android设备的唯一标识符。我已经尝试使用以下代码
String ts = Context.TELEPHONY_SERVICE;
TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(ts);
但是我知道这只适用于手机。
那么如果我的应用程序在笔记本电脑、小型笔记本电脑或其他类型的设备上运行怎么办?在这种情况下,如何获取唯一标识符?
由于MAC地址和其他与电话相关的API已经受到新的限制,只有作为系统应用程序的一部分并具备所需特权的用户才能访问这些硬件相关的唯一标识符。
来自文档:
When working with Android identifiers, follow these best practices:
Avoid using hardware identifiers. In most use cases, you can avoid using hardware identifiers, such as SSAID (Android ID), without limiting required functionality.
Android 10 (API level 29) adds restrictions for non-resettable identifiers, which include both IMEI and serial number. Your app must be a device or profile owner app, have special carrier permissions, or have the READ_PRIVILEGED_PHONE_STATE privileged permission in order to access these identifiers.
Only use an Advertising ID for user profiling or ads use cases. When using an Advertising ID, always respect users' selections regarding ad tracking. Also, ensure that the identifier cannot be connected to personally identifiable information (PII), and avoid bridging Advertising ID resets.
Use a Firebase installation ID (FID) or a privately stored GUID whenever possible for all other use cases, except for payment fraud prevention and telephony. For the vast majority of non-ads use cases, an FID or GUID should be sufficient.
Use APIs that are appropriate for your use case to minimize privacy risk. Use the DRM API for high-value content protection and the SafetyNet APIs for abuse protection. The SafetyNet APIs are the easiest way to determine whether a device is genuine without incurring privacy risk.
The remaining sections of this guide elaborate on these rules in the context of developing Android apps.
最好的情况是我们使用FID或GUID来识别每个安装的应用程序的唯一性,以下是您可以执行此操作的方法。
fun getDeviceId(): String {
return FirebaseInstallations.getInstance().id.result ?: UUID.randomUUID().toString()
}
您可以检查权限并评估它将为您提供设备ID的价值:
private static final int REQUEST_READ_PHONE_STATE = 1;
int permissionCheck = ContextCompat.checkSelfPermission(getContext(),
Manifest.permission.READ_PHONE_STATE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_READ_PHONE_STATE);
} else {
TelephonyManager tManager = (TelephonyManager) getContext().getSystemService(Context.TELEPHONY_SERVICE);
String uid = tManager.getDeviceId();
System.out.print(uid);
}
输出:358240051111110
TextView textAndroidId = (TextView)findViewById(R.id.androidid);
String AndroidId = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
textAndroidId.setText("My ID is: " + AndroidId);