Android设备有唯一的ID吗?如果有,使用Java访问它的简单方法是什么?


当前回答

Settings.Secure#ANDROID_ID返回每个用户64位十六进制字符串的唯一ANDROID ID。

import android.provider.Settings.Secure;

private String android_id = Secure.getString(getContext().getContentResolver(),
                                                        Secure.ANDROID_ID);

另请阅读唯一标识符的最佳实践:https://developer.android.com/training/articles/user-data-ids

其他回答

当设备没有电话功能时,我使用以下代码获取IMEI或使用Secure.ANDROID_ID作为替代:

String identifier = null;
TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE));
if (tm != null)
      identifier = tm.getDeviceId();
if (identifier == null || identifier .length() == 0)
      identifier = Secure.getString(activity.getContentResolver(),Secure.ANDROID_ID);

这里有30多个答案,有些是相同的,有些是独特的。这个答案是基于这些答案中的一些。其中一个是Lenn Dolling的回答。

它组合3个ID并创建32位十六进制字符串。这对我来说效果很好。

3个ID为:伪ID-根据物理设备规范生成ANDROID_ID-设置.Securite.ANDROID-ID蓝牙地址-蓝牙适配器地址

它将返回如下内容:551F27C060712A72730B0F734064B1

注意:您始终可以向longId字符串添加更多ID。例如,序列号。wifi适配器地址。伊梅。通过这种方式,您可以使每个设备都更独特。

@SuppressWarnings("deprecation")
@SuppressLint("HardwareIds")
public static String generateDeviceIdentifier(Context context) {

        String pseudoId = "35" +
                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;

        String androidId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);

        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        String btId = "";

        if (bluetoothAdapter != null) {
            btId = bluetoothAdapter.getAddress();
        }

        String longId = pseudoId + androidId + btId;

        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.update(longId.getBytes(), 0, longId.length());

            // get md5 bytes
            byte md5Bytes[] = messageDigest.digest();

            // creating a hex string
            String identifier = "";

            for (byte md5Byte : md5Bytes) {
                int b = (0xFF & md5Byte);

                // if it is a single digit, make sure it have 0 in front (proper padding)
                if (b <= 0xF) {
                    identifier += "0";
                }

                // add number to string
                identifier += Integer.toHexString(b);
            }

            // hex string to uppercase
            identifier = identifier.toUpperCase();
            return identifier;
        } catch (Exception e) {
            Log.e("TAG", e.toString());
        }
        return "";
}

TelephonyManager.getDeviceId()返回唯一的设备ID,例如,GSM的IMEI和CDMA电话的MEID或ESN。

final TelephonyManager mTelephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);            
String myAndroidDeviceId = mTelephony.getDeviceId(); 

但我建议使用:

Settings.Secure.ANDROID_ID,将ANDROID ID作为唯一的64位十六进制字符串返回。

    String   myAndroidDeviceId = Secure.getString(getApplicationContext().getContentResolver(), Secure.ANDROID_ID); 

有时TelephonyManager.getDeviceId()将返回null,因此为了确保唯一的id,您将使用以下方法:

public String getUniqueID(){    
    String myAndroidDeviceId = "";
    TelephonyManager mTelephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    if (mTelephony.getDeviceId() != null){
        myAndroidDeviceId = mTelephony.getDeviceId(); 
    }else{
         myAndroidDeviceId = Secure.getString(getApplicationContext().getContentResolver(), Secure.ANDROID_ID); 
    }
    return myAndroidDeviceId;
}

Android在Android O之后限制硬件相关Id,Android_Id是唯一Id的解决方案,但当反射设备,它将生成新的android_id以克服此问题问题是我们可以使用DRUMID。

val WIDEVINE_UUID = UUID(-0x121074568629b532L, -0x5c37d8232ae2de13L)
val drumIDByteArray = MediaDrm(WIDEVINE_UUID).getPropertyByteArray(MediaDrm.PROPERTY_DEVICE_UNIQUE_ID)

val drumID = android.util.Base64.encodeToString(drumIDByteArray,android.util.Base64.DEFAULT)

包com.aapbd.appbajarlib.common;import android.os.Build;导入java.util.Locale;导入java.util.UUID;公共类DeviceID{公共静态字符串getDeviceLanguage(){Locale Locale=Locale.getDefault();return locale.getDisplayLanguage();}公共静态字符串getDeviceCountry(){Locale Locale=Locale.getDefault();return locale.getDisplayCountry();}公共静态字符串getDeviceName(){字符串制造商=Build.manufacturer;字符串模型=Build.model;if(model.startsWith(制造商)){回报资本化(模型);}其他{回报资本化(制造商)+“”+模型;}}公共静态字符串getAndroidVersion(){字符串释放=Build.VERSION.release;int sdkVersion=内部版本.VERSION.SDK_int;return sdkVersion+“(”+release+“)”;}公共静态int getAndroidAPILevel(){int sdkVersion=内部版本.VERSION.SDK_int;return sdkVersion;}私有静态字符串大写(字符串s){如果(s==null||s.length()==0){return“”;}char first=s.charAt(0);if(字符.isUpperCase(第一个)){返回s;}其他{return Character.toUpperCase(first)+s.substring(1);}}/***返回伪唯一ID*@返回ID*/公共静态字符串getUniquePsuedoID(){//如果所有其他操作都失败,如果用户的API低于API 9(低于//而不是姜饼),已重置其设备或“安全.ANDROID_ID”//返回“null”,则返回的ID将仅基于//关闭他们的Android设备信息。这就是碰撞的地方//可能发生。//谢谢http://www.pocketmagic.net/?p=1662!//尽量不要使用DISPLAY、HOST或ID-这些项目可能会更改。//如果发生冲突,则会出现重叠数据字符串m_szDevIDShort=“35”+(Build.BOARD.length()%10)+(Build.BRAND.length)%10()+(Build.CPU_ABI.length;//感谢@Roman SL!// http://stackoverflow.com/a/4789483/950427//只有API>=9的设备具有android.os.Build.SERIAL// http://developer.android.com/reference/android/os/Build.html#SERIAL//如果用户升级软件或根目录他们的设备,将有一个重复条目字符串序列=空;尝试{serial=android.os.Build.class.getField(“serial”).get(null).toString();//继续并返回api=>9的序列号返回新UUID(m_szDevIDShort.hashCode(),serial.hashCode)).toString();}catch(异常异常){//字符串需要初始化serial=“serial”;//一些价值}//谢谢@乔!// http://stackoverflow.com/a/2853253/950427//最后,通过使用UUID类组合我们找到的值来创建唯一标识符返回新UUID(m_szDevIDShort.hashCode(),serial.hashCode)).toString();}}

我正在使用这个,它在过去的6年中一直有效。

这是图书馆:https://github.com/nillbiplob/AppBajarLIB