• QQ
  • nahooten@sina.com
  • 常州市九洲新世界花苑15-2

Android

Android 获取Device Id

原创内容,转载请注明原文网址:http://homeqin.cn/a/wenzhangboke/jishutiandi/Android/2019/0918/647.html

 
 
 
##若何常州手游开发获取Android建筑唯一ID?
 
 
 
###问题 每一个android建筑都有唯一ID吗?要是有?怎么用java非常简略获得呢?
 
 
 
###回答1(非常佳)
 
 
 
若何获得android唯一码?
 
 
 
好处:
 
 
 
1.不需求特定权限.
 
2.在99.5% Android装配(包含root过的)上,即API => 9,包管唯一性.
 
3.重装app以后仍能获得相像唯一值.
 
伪代码:
 
 
 
if API => 9/10: (99.5% of devices)
 
 
 
return unique ID containing serial id (rooted devices may be different)
 
 
 
else
 
 
 
return unique ID of build information (may overlap data - API < 9)
 
代码:
 
 
 
/**
 
 * Return pseudo unique ID
 
 * @return ID
 
 */public static String getUniquePsuedoID() {
 
    // If all else fails, if the user does have lower than API 9 (lower
 
    // than Gingerbread), has reset their device or 'Secure.ANDROID_ID'
 
    // returns 'null', then simply the ID returned will be solely based
 
    // off their Android device information. This is where the collisions
 
    // can happen.
 
    // Thanks http://www.pocketmagic.net/?p=1662!
 
    // Try not to use DISPLAY, HOST or ID - these items could change.
 
    // If there are collisions, there will be overlapping data
 
    String m_szDevIDShort = "35" + (Build.BOARD.length() % 10) + (Build.BRAND.length() % 10) + (Build.CPU_ABI.length() % 10) + (Build.DEVICE.length() % 10) + (Build.MANUFACTURER.length() % 10) + (Build.MODEL.length() % 10) + (Build.PRODUCT.length() % 10);
 
 
 
    // Thanks to @Roman SL!
 
    // http://stackoverflow.com/a/4789483/950427
 
    // Only devices with API >= 9 have android.os.Build.SERIAL
 
    // http://developer.android.com/reference/android/os/Build.html#SERIAL
 
    // If a user upgrades software or roots their device, there will be a duplicate entry
 
    String serial = null;
 
    try {
 
        serial = android.os.Build.class.getField("SERIAL").get(null).toString();
 
 
 
        // Go ahead and return the serial for api => 9
 
        return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
 
    } catch (Exception exception) {
 
        // String needs to be initialized
 
        serial = "serial"; // some value
 
    }
 
 
 
    // Thanks @Joe!
 
    // http://stackoverflow.com/a/2853253/950427
 
    // Finally, combine the values we have found by using the UUID class to create a unique identifier
 
    return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();}
 
###常州游戏开发培训回答2 好处:
 
 
 
1.不需求特定权限.
 
2.在100% Android装配(包含root过的)上,包管唯一性.
 
缺点
 
 
 
1.重装app以后不行获得相像唯一值.
 
private static String uniqueID = null;
 
private static final String PREF_UNIQUE_ID = "PREF_UNIQUE_ID";
 
 
 
public synchronized static String id(Context context) {
 
    if (uniqueID == null) {
 
        SharedPreferences sharedPrefs = context.getSharedPreferences(
 
                PREF_UNIQUE_ID, Context.MODE_PRIVATE);
 
        uniqueID = sharedPrefs.getString(PREF_UNIQUE_ID, null);
 
        if (uniqueID == null) {
 
            uniqueID = UUID.randomUUID().toString();
 
            Editor editor = sharedPrefs.edit();
 
            editor.putString(PREF_UNIQUE_ID, uniqueID);
 
            editor.co毫米it();
 
        }
 
    }
 
    return uniqueID;
 
}
 
###回答3(需求有电话卡)
 
 
 
好处: 1.重装app以后仍能获得手机App外包相像唯一值.
 
 
 
代码:
 
 
 
    final TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
 
    final String tmDevice, tmSerial, androidId;
 
    tmDevice = "" + tm.getDeviceId();
 
    tmSerial = "" + tm.getSimSerialNumber();
 
    androidId = "" + android.provider.Settings.Secure.getString(getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
 
    UUID deviceUuid = new UUID(androidId.hashCode(), ((long)tmDevice.hashCode() << 32) | tmSerial.hashCode());
 
    String deviceId = deviceUuid.toString();
 
服膺:要获得如下权限
 
 
 
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
 

上篇:上一篇:(AccessibilityService)Android辅助功效条记
下篇:下一篇:Android中微信抢红包助手的实现(代码整顿)