Android设备是否有唯一的ID,如果有的话,使用Java访问它的简单方法是什么?
TelephonyManager
和ANDROID_ID
,将Android操作系统设备的唯一设备ID作为字符串获取的方法是:String deviceId;
final TelephonyManager mTelephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if (mTelephony.getDeviceId() != null) {
deviceId = mTelephony.getDeviceId();
}
else {
deviceId = Secure.getString(
getApplicationContext().getContentResolver(),
Secure.ANDROID_ID);
}
但我强烈建议使用Google提出的一种方法,参见《识别应用安装》。
IMEI(国际移动设备识别码)对于安卓或其他移动设备来说是独一无二的。
以下是我生成唯一标识的方法:
public static String getDeviceId(Context ctx)
{
TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
String tmDevice = tm.getDeviceId();
String androidId = Secure.getString(ctx.getContentResolver(), Secure.ANDROID_ID);
String serial = null;
if(Build.VERSION.SDK_INT > Build.VERSION_CODES.FROYO) serial = Build.SERIAL;
if(tmDevice != null) return "01" + tmDevice;
if(androidId != null) return "02" + androidId;
if(serial != null) return "03" + serial;
// other alternatives (i.e. Wi-Fi MAC, Bluetooth MAC, etc.)
return null;
}
我使用以下代码来获取IMEI
或者在设备没有电话功能时使用Secure.ANDROID_ID
作为替代方案:
String identifier = null;
TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE));
if (tm != null)
identifier = tm.getDeviceId();
if (identifier == null || identifier .length() == 0)
identifier = Secure.getString(activity.getContentResolver(),Secure.ANDROID_ID);
另一种方法是在没有任何权限的应用程序中使用/sys/class/android_usb/android0/iSerial
。
user@creep:~$ adb shell ls -l /sys/class/android_usb/android0/iSerial
-rw-r--r-- root root 4096 2013-01-10 21:08 iSerial
user@creep:~$ adb shell cat /sys/class/android_usb/android0/iSerial
0A3CXXXXXXXXXX5
在Java中,您只需要使用FileInputStream来打开iSerial文件并读取其中的字符。一定要把它包装在异常处理程序中,因为并非所有设备都有这个文件。
以下至少是已知具有此文件且可以被任何应用程序读取的设备:
您还可以查看我的博客文章“将Android硬件序列号泄露给非特权应用程序”,其中我讨论了其他可用于获取信息的文件。
检测特定 Android 设备的硬件识别,您可以检查 MAC 地址。
您可以按照以下方式进行操作:
在 AndroidManifest.xml 文件中添加以下代码:
<uses-permission android:name="android.permission.INTERNET" />
现在,在您的代码中操作:
List<NetworkInterface> interfacesList = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface interface : interfacesList) {
// This will give you the interface MAC ADDRESS
interface.getHardwareAddress();
}
在每个Android设备中,至少有一个“wlan0”接口,它是WI-FI芯片。 即使WI-FI未打开,此代码也可以正常工作。
P.S. 列表中还包含其他接口,其中包含MAC地址,但这可能因手机而异。
Google Instance ID(GCM实例ID)
2015年的I/O大会上发布;在Android系统中需要Play服务7.5及以上版本。
https://developers.google.com/instance-id/
https://developers.google.com/instance-id/guides/android-implementation
InstanceID iid = InstanceID.getInstance( context ); // Google docs are wrong - this requires context
String id = iid.getId(); // blocking call
TelephonyManager.getDeviceId() 返回唯一的设备ID,例如GSM手机的IMEI和CDMA手机的MEID或ESN。
final TelephonyManager mTelephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String myAndroidDeviceId = mTelephony.getDeviceId();
但我建议使用:
Settings.Secure.ANDROID_ID,它返回Android ID作为唯一的64位十六进制字符串。
String myAndroidDeviceId = Secure.getString(getApplicationContext().getContentResolver(), Secure.ANDROID_ID);
public String getUniqueID(){
String myAndroidDeviceId = "";
TelephonyManager mTelephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if (mTelephony.getDeviceId() != null){
myAndroidDeviceId = mTelephony.getDeviceId();
}else{
myAndroidDeviceId = Secure.getString(getApplicationContext().getContentResolver(), Secure.ANDROID_ID);
}
return myAndroidDeviceId;
}
1. 使用电话管理器来获取唯一的设备ID(例如IMEI)。请参考以下示例:
import android.telephony.TelephonyManager;
import android.content.Context;
// ...
TelephonyManager telephonyManager;
telephonyManager = (TelephonyManager) getSystemService(Context.
TELEPHONY_SERVICE);
/*
* getDeviceId() returns the unique device ID.
* For example,the IMEI for GSM and the MEID or ESN for CDMA phones.
*/
String deviceId = telephonyManager.getDeviceId();
/*
* getSubscriberId() returns the unique subscriber ID,
*/
String subscriberId = telephonyManager.getSubscriberId();
android.permission.READ_PHONE_STATE
权限,这很难在你开发的应用程序中进行合理的解释。android.os.Build.SERIAL
获得的唯一设备ID。有些具有电话服务的手机也可以定义序列号。由于不是所有Android设备都有序列号,因此该解决方案并不可靠。Settings.Secure.ANDROID_ID
获得。它是一个64位数字,应在设备的使用寿命内保持不变。 ANDROID_ID
对于智能手机和平板电脑而言是一个好选择作为唯一设备标识符。要检索该值,您可以使用以下代码,
String androidId = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
但是,如果在设备上执行工厂重置,则该值可能会更改。对于来自制造商的流行手持设备存在已知的错误,其中每个实例都具有相同的ANDROID_ID
。显然,该解决方案并不是100%可靠。SharedPreferences sharedPrefs = context.getSharedPreferences(
PREF_UNIQUE_ID, Context.MODE_PRIVATE);
uniqueID = sharedPrefs.getString(PREF_UNIQUE_ID, null);
更新:由于Google推出的隐私更新,安卓10版本后选项#1和#2不再可用,因为选项2和3需要关键权限。DeviceInfoProvider
不是 Android SDK 的一部分。 - user924为了了解 Android 设备中可用的唯一标识符,请参考官方指南。
唯一标识符的最佳实践:
使用 IMEI、Mac 地址、Instance Id、GUIDs、SSAID、Advertising Id 和 Safety Net API 来验证设备。
https://developer.android.com/training/articles/user-data-ids
ANDROID_ID
,请务必阅读这个答案和这个漏洞。 - Dheeraj Vepakomma