How To Uniquely Identify An Android Device

Tuesday, April 20, 2010 01:13 AM

After doing a little bit of native iPhone application development it's pretty nifty to be able to get the unique identifier for an iPhone. When porting an iPhone application to Android I was running into a similar scenario. Is there a unique identifier for each Android device? The answer is Yes. Unfortunately there isn't just one way to do it, there's two. The first way requires the user to approve an application permission. The second way is completely open, and requires no application permission.

The Way With An Application Permission

Add this uses-permission in your applications AndroidManifest.xml

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Context context = getApplicationContext();
TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String device_id = manager.getDeviceId();

Update: This will only work if the Android device is a phone! Big thanks to Todd Berman for the catch.

The Way Without An Application Permission

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

Update: This will only work if the device is associated with a google account! Again thanks to Todd Berman for the catch.