How To Uniquely Identify An Android Device (ANDROID_ID)

Quickly share your Android Code Snippets here...

How To Uniquely Identify An Android Device (ANDROID_ID)

Postby noobles » Wed May 27, 2009 10:44 am

How to uniquely identify an android device:


From the Android Reference page:

"public static final String ANDROID_ID

The Android ID (a unique 64-bit value) as a hex string. Identical to that obtained by calling GoogleLoginService.getAndroidId(); it is also placed here so you can get it without binding to a service."

It took me awhile to figure out how to use this since all the code on the net regarding this issue is only compatible with 1.1 and most of the things the code utilizes has since been deprecated in 1.5 of the SDK.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. import android.provider.Settings.Secure;
  3.  
  4.  
  5.  
  6. private String android_id = Secure.getString(getContext().getContentResolver(), Secure.ANDROID_ID);
  7.  
  8.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


This is perfect for storing high scores for a game for each individual user without the need for user registration.

NOTE: The emulator will return a null value so if you try to use this value in the emulator environment your application will crash.
noobles
Freshman
Freshman
 
Posts: 5
Joined: Tue Mar 10, 2009 3:47 pm

Top

Postby SlumberMachine » Wed May 27, 2009 7:13 pm

Thank you, I've been searching for someway to get a device ID or other identification for some time now.
SlumberMachine
Developer
Developer
 
Posts: 39
Joined: Tue Feb 24, 2009 9:00 pm

HTC Tattoo return null on DEVICE_ID

Postby theblackdragon19 » Mon Jan 25, 2010 3:19 pm

Hi all, Wooot... my first post. Anyway... I used the information in order to get the android_id in a hexadecimal value. But I have been testing my application on the HTC Tattoo, it returns a NULL when I am asking for a DEVICE_ID.
Is it because I am doing something wrong or, do I miss another permission or is there any other way to identify the ID of the phone?

Thanks in advance,

Ka-Sing Chou
theblackdragon19
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Jan 25, 2010 3:13 pm

Postby arberg » Tue Feb 02, 2010 1:16 pm

Does this method invocation return the same ID, as the Secure.getString?
android.provider.Settings.System.getString(context.getContentResolver(), android.provider.Settings.System.ANDROID_ID)

On my HTC Tattoo the string returned by this System.getString call, is not null. Neither is it null on any of the devices my game is running on. It is null on the emulator.
arberg
Freshman
Freshman
 
Posts: 2
Joined: Tue Oct 27, 2009 10:46 am

Re: HTC Tattoo return null on DEVICE_ID

Postby sblantipodi » Tue Apr 06, 2010 12:18 am

theblackdragon19 wrote:Hi all, Wooot... my first post. Anyway... I used the information in order to get the android_id in a hexadecimal value. But I have been testing my application on the HTC Tattoo, it returns a NULL when I am asking for a DEVICE_ID.
Is it because I am doing something wrong or, do I miss another permission or is there any other way to identify the ID of the phone?

Thanks in advance,

Ka-Sing Chou


so, what is the best way to get this device ID?
sblantipodi
Senior Developer
Senior Developer
 
Posts: 122
Joined: Fri Mar 26, 2010 1:41 pm

I have test

Postby crokinours » Mon Apr 12, 2010 6:42 pm

SO :

for 1501 execution of :
{values in milliseconds}
Secure.getString(getContentResolver(), Secure.ANDROID_ID);
{289,199,390,195,183} ~200ms

android.provider.Settings.System.getString(getContentResolver(), android.provider.Settings.System.ANDROID_ID);
{1042,1048,833,747} ~800ms

So better Secure.getString(getContentResolver(), Secure.ANDROID_ID);

Say thx if it help you :-)
crokinours
Freshman
Freshman
 
Posts: 2
Joined: Mon Apr 12, 2010 6:34 pm

Top

Re: How To Uniquely Identify An Android Device (ANDROID_ID)

Postby tonymaro » Mon Mar 21, 2011 10:23 pm

I know this is an old thread, but thought I'd correct a misconception.

Using:
Secure.getString(getContentResolver(), Secure.ANDROID_ID);

CANNOT be considered truly unique. In fact, every single Viewsonic tablet appears to return the same DeviceID. That makes playing games like Pocket Empires interesting because it uses that field to identify a user account without needing a login :-) Many other games just flat won't let me play because they say an account is already associated with my device and require me to login with those credentials.

Moral of the story - it's great as a second identifier, but a better answer is to perhaps concatenate the email account name with the deviceid for a truly unique (or still semi-unique) identifier.

I've got an example of getting a list of account address from the device on my blog:
http://www.ossramblings.com/finding_goo ... d_versions
tonymaro
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Mar 21, 2011 10:19 pm

Re: How To Uniquely Identify An Android Device (ANDROID_ID)

Postby crokinours » Mon Mar 21, 2011 11:32 pm

I'm wake up by your answer, thanks for this precision !

I love google but android is very boring on his OS ! total failure !
Next phone will be iphone...
crokinours
Freshman
Freshman
 
Posts: 2
Joined: Mon Apr 12, 2010 6:34 pm

Re: How To Uniquely Identify An Android Device (ANDROID_ID)

Postby Mike.Adams » Wed Apr 06, 2011 5:09 pm

very nice thanks for that!
Hi I'm Mike. Checkout my source code, projects and work here and let me know what you think.

Feel free to also upload and submit your code/projects there for all to see and find!
Mike.Adams
Developer
Developer
 
Posts: 26
Joined: Mon Mar 07, 2011 9:19 pm
Location: New York

Re: How To Uniquely Identify An Android Device (ANDROID_ID)

Postby Openxcell » Tue Apr 12, 2011 9:54 am

noobles wrote:How to uniquely identify an android device:


From the Android Reference page:

"public static final String ANDROID_ID

The Android ID (a unique 64-bit value) as a hex string. Identical to that obtained by calling GoogleLoginService.getAndroidId(); it is also placed here so you can get it without binding to a service."

It took me awhile to figure out how to use this since all the code on the net regarding this issue is only compatible with 1.1 and most of the things the code utilizes has since been deprecated in 1.5 of the SDK.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import android.provider.Settings.Secure;
  2.  
  3. private String android_id = Secure.getString(getContext().getContentResolver(), Secure.ANDROID_ID);
  4.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


This is perfect for storing high scores for a game for each individual user without the need for user registration.

NOTE: The emulator will return a null value so if you try to use this value in the emulator environment your application will crash.


You can identify Andriod Device by this statement also.

import android.provider.Settings.System;
String android_id = System.getString(this.getContentResolver(), System.ANDROID_ID);
Openxcell
Experienced Developer
Experienced Developer
 
Posts: 79
Joined: Mon Mar 28, 2011 1:59 pm

Re: How To Uniquely Identify An Android Device (ANDROID_ID)

Postby ophidian » Fri Apr 22, 2011 6:38 am

I'm actually looking for something like a anonymous 'userid'. A device id on its own does not allow the transfer of a paying account for instance?
ophidian
Freshman
Freshman
 
Posts: 2
Joined: Thu Apr 21, 2011 7:54 pm

Re: How To Uniquely Identify An Android Device (ANDROID_ID)

Postby Hsincpro » Wed Nov 09, 2011 1:28 pm

You can get the UserId for an anonymous cell phone tracking user with this:

string userId = Request.AnonymousID
Hsincpro
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Nov 09, 2011 1:27 pm

Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 3 guests