1. DEVICE_ID
假設我們確實需要用到真實設備的標識,可能就需要用到DEVICE_ID。在以前,我們的Android設備是手機,這個DEVICE_ID可以同通過 TelephonyManager.getDeviceId()獲取,它根據不同的手機設備返回IMEI,MEID或者ESN碼,但它在使用的過程中會遇到很多問題:
非手機設備: 如果只帶有Wifi的設備或者音樂播放機沒有通話的硬體功能的話就沒有這個DEVICE_ID
許可權: 獲取DEVICE_ID需要READ_PHONE_STATE許可權,但如果我們只為了獲取它,沒有用到其他的通話功能,那這個許可權有點大才小用
bug:在少數的一些手機設備上,該實現有漏洞,會返回垃圾,如:zeros或者asterisks的產品
2. MAC ADDRESS
我們也可以通過手機的Wifi或者藍牙設備獲取MAC ADDRESS作為DEVICE ID,但是並不建議這麼做,因為並不是所有的設備都有Wifi,並且,如果Wifi沒有打開,那硬體設備無法返回MAC ADDRESS.
3. Serial Number
在Android 2.3可以通過android.os.Build.SERIAL獲取,非手機設備可以通過該介面獲取。
 
4. ANDROID_ID
ANDROID_ID是設備第一次啟動時產生和存儲的64bit的一個數,當設備被wipe後該數重置
 
ANDROID_ID似乎是獲取Device ID的一個好選擇,但它也有缺陷:
 
它在Android <=2.1 or Android >=2.3的版本是可靠、穩定的,但在2.2的版本並不是100%可靠的
在主流廠商生產的設備上,有一個很經常的bug,就是每個設備都會產生相同的ANDROID_ID:9774d56d682e549c
5. Installtion ID : UUID
以上四種方式都有或多或少存在的一定的局限性或者bug,在這裡,有另外一種方式解決,就是使用UUID,該方法無需訪問設備的資源,也跟裝置類型無關。
 
這 種方式是通過在程式安裝後第一次運行後生成一個ID實現的,但該方式跟設備唯一標識不一樣,它會因為不同的應用程式而產生不同的ID,而不是設備唯一 ID。因此經常用來標識在某個應用中的唯一ID(即Installtion ID),或者跟蹤應用的安裝數量。很幸運的,Google Developer Blog提供了這樣的一個框架:
 
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();
}
}
 
總結
 
綜 合以上所述,為了實現在設備上更通用的獲取設備唯一標識,我們可以實現這樣的一個類,為每個設備產生唯一的UUID,以ANDROID_ID為基礎,在獲 取失敗時以TelephonyManager.getDeviceId()為備選方法,如果再失敗,使用UUID的生成策略。
 
重申下,以下方法是生成Device ID,在大多數情況下Installtion ID能夠滿足我們的需求,但是如果確實需要用到Device 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 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();
 
}
 
}
}
}
 
}


 
public UUID getDeviceUuid() {
return uuid;
}
}
 
 
如何獲取Android手機的唯一標識?
代碼: 這裡是你在Android裡讀出 唯一的 IMSI-ID / IMEI-ID 的方法。
JAVA:
String myIMSI = android.os.SystemProperties.get(android.telephony.TelephonyProperties.PROPERTY_IMSI);
// within my emulator it returns: 310995000000000
 
String myIMEI = android.os.SystemProperties.get(android.telephony.TelephonyProperties.PROPERTY_IMEI);
// within my emulator it returns: 000000000000000
注:android.os.SystemProperties的標籤被打上@hide了,所以sdk中並不會存在。如果需要使用,需要有android的source code支援。
 
arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()