views:

3430

answers:

3

I need to use a unique ID for an Android app and I thought the serial number for the device would be a good candidate. How do I retrieve the serial number of an Android device in my app ?

+3  A: 
String deviceId = Settings.System.getString(getContentResolver(),
                                Settings.System.ANDROID_ID);

Although, it is not guaranteed that the Android ID will be an unique identifier.

Anthony Forloney
@Anthony its returns me null
PM - Paresh Mayani
@Paresh Mayani, It's hard to tell what may be the issue without looking at the code. My only assumption is that `getContentResolver` is returning `null`. However, it may be worth while opening a question and posting your code.
Anthony Forloney
The emulator returns a null value, but I get a reasonable result when I run this code on my nexus one
Mike
+7  A: 
TelephonyManager tManager = (TelephonyManager)myActivity.getSystemService(Context.TELEPHONY_SERVICE);
String uid = tManager.getDeviceId();

getSystemService is a method from the Activity class. getDeviceID() will return the MDN or MEID of the device depending on which radio the phone uses (GSM or CDMA).

Each device MUST return a unique value here (assuming it's a phone). This should work for any Android device with a sim slot or CDMA radio. You're on your own with that Android powered microwave ;-)

haseman
@Hasemam This is not working for me, giving "Force Close" error
PM - Paresh Mayani
@Hasemam its running fine now after adding <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission> permission in androidManifest.xml file.
PM - Paresh Mayani
+2  A: 

Don't forget to add

android:name="android.permission.READ_PHONE_STATE"

to your manifest

Michael SIlveus