[英]Android get unique id for device
I need to restrict app subscription to certain number of devices per user. 我需要将应用程序订阅限制为每个用户一定数量的设备。 For this I was trying to get unique id for android phone ( Hardware ).
为此,我试图获取Android手机的唯一ID(硬件)。 I found below options but all of them has certain problems.
我发现以下选项,但所有选项都有某些问题。 Is there any solutions which does'nt have these problems ?
有没有解决这些问题的解决方案?
After searching and thinking I come up with below solution. 经过搜索和思考,我提出了以下解决方案。
MacID can be used as the unique id. MacID可以用作唯一ID。 MacID can be get using permission
android.permission.READ_PHONE_STATE
even with Wifi switch off state. 即使在Wifi关机状态下,也可以使用权限
android.permission.READ_PHONE_STATE
获得MacID。 I have successfully tested these in few devices and it works fine. 我已经在少数设备中成功测试了这些设备,并且工作正常。 My testing is in progress will update here soon.
我的测试正在进行中,很快会在这里更新。
public static String getDeviceMacId(){
try {
List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface nif : all) {
if (!nif.getName().equalsIgnoreCase("wlan0")) continue;
byte[] macBytes = nif.getHardwareAddress();
if (macBytes == null) {
return "";
}
StringBuilder res1 = new StringBuilder();
for (byte b : macBytes) {
res1.append(String.format("%02X:",b));
}
if (res1.length() > 0) {
res1.deleteCharAt(res1.length() - 1);
}
return res1.toString();
}
} catch (Exception ex) {
//handle exception
}
return "";
}
Reference : https://stackoverflow.com/a/35830358/2788009 参考: https : //stackoverflow.com/a/35830358/2788009
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.