简体   繁体   中英

Android device Id generation

I'm working on an android project in which the activation process is done against the device Id . So I have had to generate a unique id for each device. I got two ways by surfing the net.

  • Using TelephonyManager

In this case I generate the ID as below.

TelephonyManager tManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    String uid = tManager.getDeviceId();

But it gave a combination of "0" for emulator and a unique value for the actual device.

  • And the second way is as below

     String android_id = Secure.getString(Main.this.getContentResolver(),Secure.ANDROID_ID); 

This gave a unique value even for the emulator . So what is the different between these two IDs. which one is more preferable for my requirement.

Generating a device id is actually not as simple as it seems since the result may vary depending on the android version, manufacturer, etc.

Checkout this great post which explains the pros and cons of each method of generating a device id: http://developer.samsung.com/android/technical-docs/How-to-retrieve-the-Device-Unique-ID-from-android-device

This one will give you the unique device ID, you can go for it

import android.provider.Settings.Secure;

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

*Note that this will change on Factory Reset, Do it on your own risk! or can be altered on rooted phone!

and this article will give you all the unique information which you can retrieve from the deivce such as..

  1. IMEI (only for Android devices with Phone use; needs android.permission.READ_PHONE_STATE)
  2. Pseudo-Unique ID (for all Android devices)
  3. Android ID (can be null, can change upon factory reset, can be altered on rooted phone)
  4. WLAN MAC Address string (needs android.permission.ACCESS_WIFI_STATE)
  5. BT MAC Address string (devices with Bluetooth, needs android.permission.BLUETOOTH)
String android_id = Secure.getString(Main.this.getContentResolver(),Secure.ANDROID_ID);

gives 64-bit number (as a hex string) that is randomly generated on the device's first boot and should remain constant for the lifetime of the device (The value may change if a factory reset is performed on the device.) ANDROID_ID seems a good choice for a unique device identifier.

while

TelephonyManager tManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String uid = tManager.getDeviceId();

gives the IMEI, MEID, ESN and IMSI of the phone, which is unique to that piece of hardware which is a constant through out the lifetime of the device.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM