繁体   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