安卓设备的唯一标识符

53

我想获取Android设备的唯一标识符。我已经尝试使用以下代码

String ts = Context.TELEPHONY_SERVICE;
TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(ts);

但是我知道这只适用于手机。

那么如果我的应用程序在笔记本电脑、小型笔记本电脑或其他类型的设备上运行怎么办?在这种情况下,如何获取唯一标识符?


2
@Mudassir:所以你基本上想要的是一些“铁板钉钉”,“硬编码”的东西,它永远不会改变,并且是100%可靠的吗?英特尔试过了...阅读这篇文章schneier.com/essay-187.html - Squonk
@MisterSquonk 我想要类似的东西。我喜欢“set in stone”。 :-) - Mudassir
@Mudassir:祝你好运,但如果你能找到它,我会感到惊讶。最多,我会从最不可能被更改/黑客攻击的事物原则出发,用作为尽可能“独特”的基础,然后尝试编写代码来弥补沿途遇到的任何问题。 - Squonk
已经在 https://dev59.com/CnE95IYBdhLWcg3wadOq 上提问。 - rds
2
@rds:我知道这个问题已经被问过了,而且我也尝试了之前帖子中提供的解决方案,正如我在问题中所提到的。但我的标准是不同的。 - Mudassir
@rds:您链接的问题是关于检索安卓序列号的具体问题。而这个问题则是关于获取一种在非安卓设备上也可用的ID的具体问题。 - Greg Sansom
13个回答

0

由于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()
}

2
重新安装应用程序后,它已经发生了变化。 - shubomb
而且强制开发者使用FCM仅仅为了生成ID也是愚蠢的,这些ID远不如硬件ID可靠。 - TheRealChx101

0

您可以检查权限并评估它将为您提供设备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


0
TextView textAndroidId = (TextView)findViewById(R.id.androidid);
String AndroidId = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
textAndroidId.setText("My ID is: " + AndroidId);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接