2010-06-25 65 views
8

如何从Android手机获取唯一ID?Android独特ID

每当我尝试从电话中获取唯一ID作为字符串时,它总是显示安卓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的测试。

+0

可能重复的有一个独特的Android设备ID?](http://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id) – 2011-10-11 12:01:49

+0

请参阅 http://stackoverflow.com/questions/2785485/is -there-A-独特功能的Android-DE副ID – MatejC 2010-08-16 09:10:11

+0

拿到Android手机ID最好的办法是说[这里] [1] [1]:http://stackoverflow.com/questions/4468248/unique-id-of-android-设备/ 23196094#23196094 – Nepster 2014-04-21 11:15:57

回答

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

与舱单

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

编辑:

以下是有关Android的ID的一些有趣的阅读:

How to set the Android ID

Android ID Requires Market Login

尝试将其设置为'android id'以外的内容,然后查看您是否读取了新值。

+1

感谢您的回复,但我想在getDeviceID()32位唯一ID我只得到15个字符,所以我怎么能得到Android设备的32位HMAC MD5 ID? – jaimin 2010-06-25 06:21:25

+2

请注意,此解决方案存在巨大的局限性:http://android-developers.blogspot.com/2011/03/identifying-app-installations.html – emmby 2011-04-07 20:08:04

+1

如果在没有手机的平板电脑上运行,该怎么办? – Chloe 2012-04-07 19:57:50

1

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

+0

请注意,此解决方案存在巨大的局限性:http://android-developers.blogspot.com/2011/03/identifying-app-installations.html – emmby 2011-04-07 20:06:55

+0

我测试过的很多手机(30%)在任何情况下为Mac地址返回null。 *有些*电话只能在Wifi打开时报告。 – JoJo 2012-03-09 18:17:51

2

这里是代码段如何获取androidId,唯一的DeviceId和序列号为您的Android手机可能会帮助你。

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

有关如何为您的应用程序是从安装在每台Android设备唯一标识符,看到这个官方Android开发者博客中详细说明:

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

看来最好的方法是让您在安装时自己创建一个自己的应用程序,然后在应用程序重新启动时读取它。

我个人觉得这可以接受但并不理想。没有任何一个由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(); 
    } 
} 
1
Settings.Secure.getString(contentResolver,Settings.Secure.ANDROID_ID); 

这不是一个好方法,它会在某些情况下返回null。

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

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(可能)的值,以便与工厂重置之前返回的值相比,该方法将返回不同的唯一ID。 – 2012-10-02 10:49:55

0

独特标识

Info adInfo = null; 

  

try { 

     adInfo = AdvertisingIdClient.getAdvertisingIdInfo(mContext); 

} catch (IOException e) { 

     ... 

} catch (GooglePlayServicesAvailabilityException e) { 

     ... 

} catch (GooglePlayServicesNotAvailableException e) { 

     ... 

} 

  

String AdId = adInfo.getId(); 

的[