簡體   English   中英

Android唯一ID

[英]Android unique id

如何從Android手機獲取唯一ID?

每當我嘗試從手機中獲取唯一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進行測試。

有關如何為安裝應用程序的每個Android設備獲取唯一標識符的詳細說明,請參閱此官方Android開發人員博客帖子:

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

看起來最好的方法是在安裝時生成一個自己,然后在重新啟動應用程序時讀取它。

我個人覺得這個可以接受但不理想。 Android提供的任何一個標識符都不適用於所有情況,因為大多數都依賴於手機的無線電狀態(wifi開/關,蜂窩開/關,藍牙開/關)。 其他像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();
    }
}
((TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();

與清單

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

編輯:

這是關於android id的一些有趣的閱讀:

如何設置Android ID

Android ID需要市場登錄

嘗試將其設置為“android id”以外的其他內容,並查看是否讀取了新值。

這里是代碼段如何獲得androidId,你的Android手機的唯一DeviceId和序列號可能會幫助你。

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);  
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;

}

無線MAC地址比IMEI更獨特,因為后者在被盜設備上被欺騙。 缺點是它只適用於支持WiFi的設備。 WifiInfo

唯一身份

Info adInfo = null;

 

try {

     adInfo = AdvertisingIdClient.getAdvertisingIdInfo(mContext);

} catch (IOException e) {

     ...

} catch (GooglePlayServicesAvailabilityException e) {

     ...

} catch (GooglePlayServicesNotAvailableException e) {

     ...

}

 

String AdId = adInfo.getId();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM