Android设备有唯一的ID吗?如果有,使用Java访问它的简单方法是什么?
当前回答
不建议使用,因为deviceId可以在第三方手中用作跟踪,但这是另一种方式。
@SuppressLint("HardwareIds")
private String getDeviceID() {
deviceId = Settings.Secure.getString(getApplicationContext().getContentResolver(),
Settings.Secure.ANDROID_ID);
return deviceId;
}
其他回答
您将通过使用以下代码获得wifi mac地址,无论您在尝试连接到wifi时是否使用了随机地址,也无论wifi是否打开或关闭。
我使用了下面链接中的一个方法,并添加了一个小修改,以获得准确的地址,而不是随机化的地址:
在Android 6.0中获取MAC地址
public static String getMacAddr() {
StringBuilder res1 = new StringBuilder();
try {
List<NetworkInterface> all =
Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface nif : all) {
if (!nif.getName().equalsIgnoreCase("p2p0")) continue;
byte[] macBytes = nif.getHardwareAddress();
if (macBytes == null) {
continue;
}
res1 = new StringBuilder();
for (byte b : macBytes) {
res1.append(String.format("%02X:",b));
}
if (res1.length() > 0) {
res1.deleteCharAt(res1.length() - 1);
}
}
} catch (Exception ex) {
}
return res1.toString();
}
不建议使用,因为deviceId可以在第三方手中用作跟踪,但这是另一种方式。
@SuppressLint("HardwareIds")
private String getDeviceID() {
deviceId = Settings.Secure.getString(getApplicationContext().getContentResolver(),
Settings.Secure.ANDROID_ID);
return deviceId;
}
官方Android开发者博客现在有一篇关于这个主题的完整文章,即“识别应用程序安装”。
包com.aapbd.appbajarlib.common;import android.os.Build;导入java.util.Locale;导入java.util.UUID;公共类DeviceID{公共静态字符串getDeviceLanguage(){Locale Locale=Locale.getDefault();return locale.getDisplayLanguage();}公共静态字符串getDeviceCountry(){Locale Locale=Locale.getDefault();return locale.getDisplayCountry();}公共静态字符串getDeviceName(){字符串制造商=Build.manufacturer;字符串模型=Build.model;if(model.startsWith(制造商)){回报资本化(模型);}其他{回报资本化(制造商)+“”+模型;}}公共静态字符串getAndroidVersion(){字符串释放=Build.VERSION.release;int sdkVersion=内部版本.VERSION.SDK_int;return sdkVersion+“(”+release+“)”;}公共静态int getAndroidAPILevel(){int sdkVersion=内部版本.VERSION.SDK_int;return sdkVersion;}私有静态字符串大写(字符串s){如果(s==null||s.length()==0){return“”;}char first=s.charAt(0);if(字符.isUpperCase(第一个)){返回s;}其他{return Character.toUpperCase(first)+s.substring(1);}}/***返回伪唯一ID*@返回ID*/公共静态字符串getUniquePsuedoID(){//如果所有其他操作都失败,如果用户的API低于API 9(低于//而不是姜饼),已重置其设备或“安全.ANDROID_ID”//返回“null”,则返回的ID将仅基于//关闭他们的Android设备信息。这就是碰撞的地方//可能发生。//谢谢http://www.pocketmagic.net/?p=1662!//尽量不要使用DISPLAY、HOST或ID-这些项目可能会更改。//如果发生冲突,则会出现重叠数据字符串m_szDevIDShort=“35”+(Build.BOARD.length()%10)+(Build.BRAND.length)%10()+(Build.CPU_ABI.length;//感谢@Roman SL!// http://stackoverflow.com/a/4789483/950427//只有API>=9的设备具有android.os.Build.SERIAL// http://developer.android.com/reference/android/os/Build.html#SERIAL//如果用户升级软件或根目录他们的设备,将有一个重复条目字符串序列=空;尝试{serial=android.os.Build.class.getField(“serial”).get(null).toString();//继续并返回api=>9的序列号返回新UUID(m_szDevIDShort.hashCode(),serial.hashCode)).toString();}catch(异常异常){//字符串需要初始化serial=“serial”;//一些价值}//谢谢@乔!// http://stackoverflow.com/a/2853253/950427//最后,通过使用UUID类组合我们找到的值来创建唯一标识符返回新UUID(m_szDevIDShort.hashCode(),serial.hashCode)).toString();}}
我正在使用这个,它在过去的6年中一直有效。
这是图书馆:https://github.com/nillbiplob/AppBajarLIB
正如DaveWebb提到的,Android开发者博客有一篇文章介绍了这一点。他们的首选解决方案是跟踪应用程序安装而不是设备,这对于大多数使用情况都很有效。博客文章将向您展示实现这一功能所需的代码,我建议您查看一下。
然而,如果您需要设备标识符而不是应用程序安装标识符,博客文章将继续讨论解决方案。我与谷歌的某位人士进行了交谈,以获得一些额外的澄清,以防您需要这样做。以下是我发现的有关设备标识符的信息,这些信息在上述博客文章中没有提及:
ANDROID_ID是首选设备标识符。ANDROID_ID在ANDROID<=2.1或>=2.3版本上非常可靠。只有2.2存在帖子中提到的问题。多个制造商的多个设备受到2.2中ANDROID_ID错误的影响。据我所知,所有受影响的设备都具有相同的ANDROID_ID,即9774d56d682e549c。顺便说一下,这也是模拟器报告的相同设备id。谷歌相信,原始设备制造商已经为他们的许多或大部分设备修补了这个问题,但我能够证实,至少在2011年4月初,找到ANDROID_ID损坏的设备仍然很容易。
根据谷歌的建议,我实现了一个类,该类将为每个设备生成一个唯一的UUID,在适当的情况下使用ANDROID_ID作为种子,必要时返回TelephonyManager.getDeviceId(),如果失败,则使用随机生成的唯一UUID,该UUID将在应用程序重新启动(但不是应用程序重新安装)期间保持。
请注意,对于必须在设备ID上回退的设备,唯一ID将在出厂重置期间保持。这是需要注意的。如果您需要确保出厂重置将重置您的唯一ID,您可能需要考虑直接返回到随机UUID而不是设备ID。
同样,此代码用于设备ID,而不是应用安装ID。对于大多数情况,应用安装ID可能是您要查找的。但是,如果您确实需要设备ID,那么下面的代码可能适用于您。
import android.content.Context;
import android.content.SharedPreferences;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;
import java.io.UnsupportedEncodingException;
import java.util.UUID;
public class DeviceUuidFactory {
protected static final String PREFS_FILE = "device_id.xml";
protected static final String PREFS_DEVICE_ID = "device_id";
protected volatile static UUID uuid;
public DeviceUuidFactory(Context context) {
if (uuid == null) {
synchronized (DeviceUuidFactory.class) {
if (uuid == null) {
final SharedPreferences prefs = context
.getSharedPreferences(PREFS_FILE, 0);
final String id = prefs.getString(PREFS_DEVICE_ID, null);
if (id != null) {
// Use the ids previously computed and stored in the
// prefs file
uuid = UUID.fromString(id);
} else {
final String androidId = Secure.getString(
context.getContentResolver(), Secure.ANDROID_ID);
// Use the Android ID unless it's broken, in which case
// fallback on deviceId,
// unless it's not available, then fallback on a random
// number which we store to a prefs file
try {
if (!"9774d56d682e549c".equals(androidId)) {
uuid = UUID.nameUUIDFromBytes(androidId
.getBytes("utf8"));
} else {
final String deviceId = (
(TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE))
.getDeviceId();
uuid = deviceId != null ? UUID
.nameUUIDFromBytes(deviceId
.getBytes("utf8")) : UUID
.randomUUID();
}
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
// Write the value out to the prefs file
prefs.edit()
.putString(PREFS_DEVICE_ID, uuid.toString())
.commit();
}
}
}
}
}
/**
* Returns a unique UUID for the current android device. As with all UUIDs,
* this unique ID is "very highly likely" to be unique across all Android
* devices. Much more so than ANDROID_ID is.
*
* The UUID is generated by using ANDROID_ID as the base key if appropriate,
* falling back on TelephonyManager.getDeviceID() if ANDROID_ID is known to
* be incorrect, and finally falling back on a random UUID that's persisted
* to SharedPreferences if getDeviceID() does not return a usable value.
*
* In some rare circumstances, this ID may change. In particular, if the
* device is factory reset a new device ID may be generated. In addition, if
* a user upgrades their phone from certain buggy implementations of Android
* 2.2 to a newer, non-buggy version of Android, the device ID may change.
* Or, if a user uninstalls your app on a device that has neither a proper
* Android ID nor a Device ID, this ID may change on reinstallation.
*
* Note that if the code falls back on using TelephonyManager.getDeviceId(),
* the resulting ID will NOT change after a factory reset. Something to be
* aware of.
*
* Works around a bug in Android 2.2 for many devices when using ANDROID_ID
* directly.
*
* @see http://code.google.com/p/android/issues/detail?id=10603
*
* @return a UUID that may be used to uniquely identify your device for most
* purposes.
*/
public UUID getDeviceUuid() {
return uuid;
}
}
推荐文章
- Android:为什么视图没有maxHeight ?
- 如何获得具有已知资源名称的资源id ?
- 在Android上将字符串转换为整数
- 为什么“System.out。”println“工作在Android?
- WebView显示err_cleartext_not_allowed尽管站点是HTTPS
- Android M权限:对shouldShowRequestPermissionRationale()函数的使用感到困惑
- 为什么构建类型与产品风味不同?
- 我如何在一个片段中访问getSupportFragmentManager() ?
- 调试在哪里。Android Studio中的密钥存储库
- 从资产中读取文件
- 在不活动的地方调用getLayoutInflater()
- 设置Android布局元素的背景颜色
- 错误:'keytool'不能被识别为内部或外部命令、可操作程序或批处理文件
- 在应用程序本身中更改Locale
- apk (.apk)和应用程序包(.aab)的区别