来自本地的Android O设备序列号 [英] Android O device serial number from native

查看:345
本文介绍了来自本地的Android O设备序列号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Android O上的本机获取序列号而不调用Java Build.getSerial()的正确方法是什么.

What is the proper way to get the serial number from native on Android O without calling the Java Build.getSerial().

在Android上<来自本机的26个版本,我们可以使用以下代码获取设备序列号:

On Android < 26 versions from native we could get the device serial number using the following code:

string serial = read_property("ro.boot.serialno");

...

string read_property(const string& property_name) {
  char propertyValue[PROP_VALUE_MAX];
  int propertyLen = __system_property_get(property_name.c_str(), propertyValue);
  ...
}

在Android O上,这会引发错误:

On Android O this throws an error:

Access denied finding property "ro.boot.serialno"

尽管授予了READ_PHONE_STATE权限.似乎与Android 26中已弃用的Build.SERIAL有关.

Although the READ_PHONE_STATE permission is granted. Seems to be related to the deprecated Build.SERIAL in Android 26.

我设法使用adb来获取此属性,因此该值不会被删除并且存在:

I managed to get this property using adb, so the value is not removed and is there:

adb shell getprop ro.boot.serialno

推荐答案

您可以使用Build.getSerial()获取序列号.

You can use Build.getSerial() to get the serial number.

如果用户已授予READ_PHONE_STATE权限,那么就没有问题,但是如果用户撤消了该权限或从未授予过该权限,则应考虑到在尝试检索它时会在运行时收到SecurityException.

If the user has granted the READ_PHONE_STATE permission then there is no problem but if the user revoked the permission or has never granted it - you should take into consideration that you will get a SecurityException at runtime when trying to retrieve it.

总体而言-过去几年来,Google似乎在努力阻止应用使用个人可识别信息(例如设备的序列号),因此您应该考虑安装ID之类的替代方法.

In general - it looks like Google is slowly pushing to prevent apps from using personal identifiable information (like serial numbers of device) over the last few years so and you should look into alternatives like installationId.

这篇关于来自本地的Android O设备序列号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆