Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a unique Android device ID?

Do Android devices have a unique ID, and if so, what is a simple way to access it using Java?

like image 326
Tyler Avatar asked May 07 '10 00:05

Tyler


People also ask

Do Android devices have a unique ID?

Secure#ANDROID_ID returns the Android ID as an unique for each user 64-bit hex string. It's known to be null sometimes, it's documented as "can change upon factory reset". Use at your own risk, and it can be easily changed on a rooted phone.

Is device ID always same?

Android identifiers can have varying lifespans, but the lifespan is usually related to how the ID is reset: Session-only: A new ID is used every time the user restarts the app. Install-reset: A new ID is used every time user uninstalls and reinstalls the app.

What is unique device id?

There are several ways to know your Android Device ID 2- Another way to find the ID is by going to the Menu >Settings > About Phone > Status. The IMEI / IMSI / MEID should be present in the phone status setting. 3- The ID could also be below or under the battery or on the back or bottom of the device itself.

How can you identify a uniquely mobile device?

Use UUID UUID. randomUUID() method generates an unique identifier for a specific installation. You have just to store that value and your user will be identified at the next launch of your application.


2 Answers

Settings.Secure#ANDROID_ID returns the Android ID as an unique for each user 64-bit hex string.

import android.provider.Settings.Secure;  private String android_id = Secure.getString(getContext().getContentResolver(),                                                         Secure.ANDROID_ID); 

Also read Best practices for unique identifiers: https://developer.android.com/training/articles/user-data-ids

like image 65
Anthony Forloney Avatar answered Sep 22 '22 11:09

Anthony Forloney


UPDATE: As of recent versions of Android, many of the issues with ANDROID_ID have been resolved, and I believe this approach is no longer necessary. Please take a look at Anthony's answer.

Full disclosure: my app used the below approach originally but no longer uses this approach, and we now use the approach outlined in the Android Developer Blog entry that emmby's answer links to (namely, generating and saving a UUID#randomUUID()).


There are many answers to this question, most of which will only work "some" of the time, and unfortunately, that's not good enough.

Based on my tests of devices (all phones, at least one of which is not activated):

  1. All devices tested returned a value for TelephonyManager.getDeviceId()
  2. All GSM devices (all tested with a SIM) returned a value for TelephonyManager.getSimSerialNumber()
  3. All CDMA devices returned null for getSimSerialNumber() (as expected)
  4. All devices with a Google account added returned a value for ANDROID_ID
  5. All CDMA devices returned the same value (or derivation of the same value) for both ANDROID_ID and TelephonyManager.getDeviceId() -- as long as a Google account has been added during setup.
  6. I did not yet have a chance to test GSM devices with no SIM, a GSM device with no Google account added, or any of the devices in airplane mode.

So if you want something unique to the device itself, TM.getDeviceId() should be sufficient. Obviously, some users are more paranoid than others, so it might be useful to hash 1 or more of these identifiers, so that the string is still virtually unique to the device, but does not explicitly identify the user's actual device. For example, using String.hashCode(), combined with a UUID:

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(); 

might result in something like: 00000000-54b3-e7c7-0000-000046bffd97

It works well enough for me.

As Richard mentions below, don't forget that you need permission to read the TelephonyManager properties, so add this to your manifest:

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

import libraries

import android.content.Context; import android.telephony.TelephonyManager; import android.view.View; 
like image 30
Joe Avatar answered Sep 21 '22 11:09

Joe