Android设备有唯一的ID吗?如果有,使用Java访问它的简单方法是什么?
当前回答
为了完整起见,以下是如何在Xamarin.Android和C#中获取Id:
var id = Settings.Secure.GetString(ContentResolver, Settings.Secure.AndroidId);
或者如果您不在“活动”中:
var id = Settings.Secure.GetString(context.ContentResolver, Settings.Secure.AndroidId);
其中上下文是传入的上下文。
其他回答
如果添加:
Settings.Secure.getString(context.contentResolver,
Settings.Secure.ANDROID_ID)
Android Lint将向您发出以下警告:
不建议使用getString获取设备标识符。检查信息:不建议使用这些设备标识符除了用于高价值欺诈预防和高级电话之外使用案例。对于广告用例,请使用AdvertisingIdClient$Info#getId,用于分析,请使用InstanceId#getId。
所以,你应该避免使用这个。
如Android开发者文档中所述:
1:避免使用硬件标识符。在大多数使用情况下,您可以避免使用硬件标识符,例如SSAID(Android ID)和IMEI,而不限制所需的功能。2:仅在用户分析或广告用例中使用广告ID。使用广告ID时,始终尊重用户的选择关于广告跟踪。此外,请确保标识符不能连接到个人身份信息(PII),并避免桥接广告ID重置。3:在所有其他用例中,尽可能使用实例ID或私有存储的GUID,但支付欺诈预防和电话除外。对于绝大多数非广告用例,实例ID或GUID应足够。4:使用适合您的用例的API,以最大限度地降低隐私风险。使用DRM API保护高价值内容用于滥用保护的SafetyNet API。SafetyNet API是确定设备是否为正品的最简单方法隐私风险。
这里有30多个答案,有些是相同的,有些是独特的。这个答案是基于这些答案中的一些。其中一个是Lenn Dolling的回答。
它组合3个ID并创建32位十六进制字符串。这对我来说效果很好。
3个ID为:伪ID-根据物理设备规范生成ANDROID_ID-设置.Securite.ANDROID-ID蓝牙地址-蓝牙适配器地址
它将返回如下内容:551F27C060712A72730B0F734064B1
注意:您始终可以向longId字符串添加更多ID。例如,序列号。wifi适配器地址。伊梅。通过这种方式,您可以使每个设备都更独特。
@SuppressWarnings("deprecation")
@SuppressLint("HardwareIds")
public static String generateDeviceIdentifier(Context context) {
String pseudoId = "35" +
Build.BOARD.length() % 10 +
Build.BRAND.length() % 10 +
Build.CPU_ABI.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 androidId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
String btId = "";
if (bluetoothAdapter != null) {
btId = bluetoothAdapter.getAddress();
}
String longId = pseudoId + androidId + btId;
try {
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
messageDigest.update(longId.getBytes(), 0, longId.length());
// get md5 bytes
byte md5Bytes[] = messageDigest.digest();
// creating a hex string
String identifier = "";
for (byte md5Byte : md5Bytes) {
int b = (0xFF & md5Byte);
// if it is a single digit, make sure it have 0 in front (proper padding)
if (b <= 0xF) {
identifier += "0";
}
// add number to string
identifier += Integer.toHexString(b);
}
// hex string to uppercase
identifier = identifier.toUpperCase();
return identifier;
} catch (Exception e) {
Log.e("TAG", e.toString());
}
return "";
}
Settings.Secure#ANDROID_ID返回每个用户64位十六进制字符串的唯一ANDROID ID。
import android.provider.Settings.Secure;
private String android_id = Secure.getString(getContext().getContentResolver(),
Secure.ANDROID_ID);
另请阅读唯一标识符的最佳实践:https://developer.android.com/training/articles/user-data-ids
IMEI怎么样。这对于Android或其他移动设备来说是独一无二的。
这里有相当有用的信息。
它包括五种不同的ID类型:
IMEI(仅适用于使用手机的Android设备;需要Android.permission.READ_Phone_STATE)伪唯一ID(适用于所有Android设备)Android ID(可以为空,可以在工厂重置时更改,可以在根手机上更改)WLAN MAC地址字符串(需要android.permission.ACCESS_WIFI_STATE)BT MAC地址字符串(带蓝牙的设备,需要android.permission.蓝牙)
推荐文章
- BottomSheetDialogFragment的圆角
- 在应用程序启动时出现“无法获得BatchedBridge,请确保您的bundle被正确打包”的错误
- 我如何改变默认对话框按钮的文本颜色在安卓5
- 更改单选按钮的圆圈颜色
- 如何在android中复制一个文件?
- adb找不到我的设备/手机(MacOS X)
- 如何在新的材质主题中改变背面箭头的颜色?
- androidviewpager与底部点
- 相同的导航抽屉在不同的活动
- 如何从视图中获得托管活动?
- 单一的TextView与多种颜色的文本
- 如何在非活动类(LocationManager)中使用getSystemService ?
- 在清单中注册应用程序类?
- Android:从数组中编程创建旋转器
- 如何在Java中创建唯一的ID ?