简体   繁体   English

Android唯一ID

[英]Android unique id

How do I get an unique ID from an Android phone? 如何从Android手机获取唯一ID?

Whenever I try to get the unique ID from the phone as a string it always shows android id and no other unique hex values. 每当我尝试从手机中获取唯一ID作为字符串时,它总是显示android id并且没有其他唯一的十六进制值。

How do I get that one? 我怎么得到那个?

This is the code I use to get the ID until now: 这是我用来获取ID的代码:

String id=Settings.Secure.getString(contentResolver,Settings.Secure.ANDROID_ID);
Log.i("Android is is:",id);

the output which I get looks like this: 我得到的输出看起来像这样:

Android id is: android id

I am using a Nexus One for testing. 我正在使用Nexus One进行测试。

For detailed instructions on how to get a Unique Identifier for each Android device your application is installed from, see this official Android Developers Blog posting: 有关如何为安装应用程序的每个Android设备获取唯一标识符的详细说明,请参阅此官方Android开发人员博客帖子:

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

It seems the best way is for you to generate one your self upon installation and subsequently read it when the application is re-launched. 看起来最好的方法是在安装时生成一个自己,然后在重新启动应用程序时读取它。

I personally find this acceptable but not ideal. 我个人觉得这个可以接受但不理想。 No one identifier provided by Android works in all instances as most are dependent on the phone's radio states (wifi on/off, cellular on/off, bluetooth on/off). Android提供的任何一个标识符都不适用于所有情况,因为大多数都依赖于手机的无线电状态(wifi开/关,蜂窝开/关,蓝牙开/关)。 The others like Settings.Secure.ANDROID_ID must be implemented by the manufacturer and are not guaranteed to be unique. 其他像Settings.Secure.ANDROID_ID必须由制造商实施,并不保证是唯一的。

The following is an example of writing data to an INSTALLATION file that would be stored along with any other data the application saves locally. 以下是将数据写入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();

with manifest 与清单

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

Edit: 编辑:

Here is some interesting reading about the android id: 这是关于android id的一些有趣的阅读:

How to set the Android ID 如何设置Android ID

Android ID Requires Market Login Android ID需要市场登录

Try setting it to something other than 'android id' and see if you read the new value. 尝试将其设置为“android id”以外的其他内容,并查看是否读取了新值。

Here is code segment how to get androidId, unique DeviceId and Serial Number for your android phone may be it helps you. 这里是代码段如何获得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);

this is not a good method it will return null in some cases. 在某些情况下,这不是一个好的方法,它将返回null。

THis piece of code will help you to generate unique pseudodevice ID .......`` 这段代码将帮助您生成唯一的伪设备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;

}

Wireless MAC address is more unique than IMEI, because the later gets spoofed on stolen devices. 无线MAC地址比IMEI更独特,因为后者在被盗设备上被欺骗。 Drawback is that it only works on WiFi enabled devices. 缺点是它只适用于支持WiFi的设备。 WifiInfo WifiInfo

Unique Id 唯一身份

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