Android唯一标识符

9

如何从安卓手机获取唯一标识符?

每当我尝试以字符串形式获取电话的唯一ID时,它总是显示android id而没有其他唯一的十六进制值。

我该如何获取那一个?

以下是我目前用于获取ID的代码:

String id=Settings.Secure.getString(contentResolver,Settings.Secure.ANDROID_ID);
Log.i("Android is is:",id);

我得到的输出结果看起来像这样:

Android id is: android id

我正在使用Nexus One进行测试。


See https://dev59.com/V3E85IYBdhLWcg3wbS1h - MatejC
可能是是否存在唯一的Android设备ID?的重复问题。 - Joachim Sauer
获取Android手机id的最佳方法在这里说明。 - Zar E Ahmer
使用这个库,通过Identity.getDeviceId(context)获取每个设备的唯一ID,始终可用。 - caw
6个回答

17

要获取每个安装了您的应用程序的Android设备的唯一标识符的详细说明,请参阅此官方Android开发者博客文章:

http://android-developers.blogspot.com/2011/03/identifying-app-installations.html

似乎最好的方法是在安装时自动生成一个标识符,然后在重新启动应用程序时读取它。

我个人认为这样做是可以接受但并不理想。由于大多数标识符都依赖于手机的无线电状态(wifi开/关,蜂窝网络开/关,蓝牙开/关),因此Android提供的没有一种标识符适用于所有情况。其他标识符例如Settings.Secure.ANDROID_ID必须由制造商实现,不能保证唯一性。

以下是将数据写入INSTALLATION文件并与应用程序本地保存的任何其他数据一起存储的示例。

public class Installation {
    private static String sID = null;
    private static final String INSTALLATION = "INSTALLATION";

    public synchronized static String id(Context context) {
        if (sID == null) {  
            File installation = new File(context.getFilesDir(), INSTALLATION);
            try {
                if (!installation.exists())
                    writeInstallationFile(installation);
                sID = readInstallationFile(installation);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return sID;
    }

    private static String readInstallationFile(File installation) throws IOException {
        RandomAccessFile f = new RandomAccessFile(installation, "r");
        byte[] bytes = new byte[(int) f.length()];
        f.readFully(bytes);
        f.close();
        return new String(bytes);
    }

    private static void writeInstallationFile(File installation) throws IOException {
        FileOutputStream out = new FileOutputStream(installation);
        String id = UUID.randomUUID().toString();
        out.write(id.getBytes());
        out.close();
    }
}

4
((TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();

使用清单文件

<uses-permission android:name='android.permission.READ_PHONE_STATE' />

编辑:

这里有一些关于Android ID的有趣阅读材料:

如何设置Android ID

Android ID需要市场登录

尝试将其设置为“android id”以外的其他内容,看看是否能读取新值。


1
谢谢您的回复,但我想在getDeviceID()中获得32位唯一标识符,而我只能获得15个字符,那么我该如何获取Android设备的32位HMAC MD5 ID? - jaimin
2
请注意,这种解决方案存在巨大的限制:http://android-developers.blogspot.com/2011/03/identifying-app-installations.html - emmby
1
如果在没有手机的平板电脑上运行该程序会怎样? - Chloe
在 Nexus 7 平板电脑上返回 null 值。 - Aamirkhan

2

以下是如何获取Android设备的Android ID、唯一设备ID和序列号的代码段,希望对您有所帮助。

TelephonyManager tm = (TelephonyManager)getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
           final String DeviceId, SerialNum, androidId;
            DeviceId = tm.getDeviceId();
            SerialNum = tm.getSimSerialNumber();
            androidId = Secure.getString(getContentResolver(),Secure.ANDROID_ID);

            UUID deviceUuid = new UUID(androidId.hashCode(), ((long)DeviceId.hashCode() << 32) | SerialNum.hashCode());
            String mydeviceId = deviceUuid.toString();
            Log.v("My Id", "Android DeviceId is: " +DeviceId); 
            Log.v("My Id", "Android SerialNum is: " +SerialNum); 
            Log.v("My Id", "Android androidId is: " +androidId);  

1
Settings.Secure.getString(contentResolver,Settings.Secure.ANDROID_ID);

这不是一个好的方法,有些情况下会返回null。

这段代码将帮助您生成唯一的伪设备ID......``

    public String getDeviceID() {

/*String Return_DeviceID = USERNAME_and_PASSWORD.getString(DeviceID_key,"Guest");
return Return_DeviceID;*/

TelephonyManager TelephonyMgr = (TelephonyManager) getApplicationContext().getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
String m_szImei = TelephonyMgr.getDeviceId(); // Requires
// READ_PHONE_STATE

// 2 compute DEVICE ID
String m_szDevIDShort = "35"
+ // we make this look like a valid IMEI
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; // 13 digits
// 3 android ID - unreliable
String m_szAndroidID = Secure.getString(getContentResolver(),Secure.ANDROID_ID);
// 4 wifi manager, read MAC address - requires
// android.permission.ACCESS_WIFI_STATE or comes as null
WifiManager wm = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
String m_szWLANMAC = wm.getConnectionInfo().getMacAddress();
// 5 Bluetooth MAC address android.permission.BLUETOOTH required
BluetoothAdapter m_BluetoothAdapter = null; // Local Bluetooth adapter
m_BluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
String m_szBTMAC = m_BluetoothAdapter.getAddress();
System.out.println("m_szBTMAC "+m_szBTMAC);

// 6 SUM THE IDs
String m_szLongID = m_szImei + m_szDevIDShort + m_szAndroidID+ m_szWLANMAC + m_szBTMAC;
System.out.println("m_szLongID "+m_szLongID);
MessageDigest m = null;
try {
m = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
                }
m.update(m_szLongID.getBytes(), 0, m_szLongID.length());
byte p_md5Data[] = m.digest();

String m_szUniqueID = new String();
for (int i = 0; i < p_md5Data.length; i++) {
int b = (0xFF & p_md5Data[i]);
// if it is a single digit, make sure it have 0 in front (proper
// padding)
if (b <= 0xF)
m_szUniqueID += "0";
// add number to string
m_szUniqueID += Integer.toHexString(b);
}
m_szUniqueID = m_szUniqueID.toUpperCase();

Log.i("-------------DeviceID------------", m_szUniqueID);
Log.d("DeviceIdCheck", "DeviceId that generated MPreferenceActivity:"+m_szUniqueID);

return m_szUniqueID;

}

1
这个方法可能是独一无二的,但不安全,因为如果用户进行了恢复出厂设置,Secure.ANDREID_ID的值可能会改变,所以该方法将返回与恢复出厂设置前返回的不同的唯一标识符。 - Ungureanu Liviu

1

无线MAC地址比IMEI更独特,因为后者在被盗设备上会被欺骗。缺点是它仅适用于启用WiFi的设备。WifiInfo


请注意,这种解决方案存在巨大的限制:http://android-developers.blogspot.com/2011/03/identifying-app-installations.html - emmby
我测试过的许多手机(30%)在任何情况下返回null作为Mac地址。一些手机只有在Wifi打开时才能报告它。 - JoJo

0

唯一标识符

Info adInfo = null;

 

try {

     adInfo = AdvertisingIdClient.getAdvertisingIdInfo(mContext);

} catch (IOException e) {

     ...

} catch (GooglePlayServicesAvailabilityException e) {

     ...

} catch (GooglePlayServicesNotAvailableException e) {

     ...

}

 

String AdId = adInfo.getId();

 


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接