Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android unique id

Tags:

android

How do I get an unique ID from an Android phone?

Whenever I try to get the unique ID from the phone as a string it always shows android id and no other unique hex values.

How do I get that one?

This is the code I use to get the ID until now:

String id=Settings.Secure.getString(contentResolver,Settings.Secure.ANDROID_ID);
Log.i("Android is is:",id);

the output which I get looks like this:

Android id is: android id

I am using a Nexus One for testing.

like image 863
jaimin Avatar asked Jun 25 '10 06:06

jaimin


4 Answers

For detailed instructions on how to get a Unique Identifier for each Android device your application is installed from, see this official Android Developers Blog posting:

http://android-developers.blogspot.com/2011/03/identifying-app-installations.html

It seems the best way is for you to generate one your self upon installation and subsequently read it when the application is re-launched.

I personally find this acceptable but not ideal. No one identifier provided by Android works in all instances as most are dependent on the phone's radio states (wifi on/off, cellular on/off, bluetooth on/off). The others like Settings.Secure.ANDROID_ID must be implemented by the manufacturer and are not guaranteed to be unique.

The following is an example of writing data to an INSTALLATION file that would be stored along with any other data the application saves locally.

public class Installation {
    private static String sID = null;
    private static final String INSTALLATION = "INSTALLATION";

    public synchronized static String id(Context context) {
        if (sID == null) {  
            File installation = new File(context.getFilesDir(), INSTALLATION);
            try {
                if (!installation.exists())
                    writeInstallationFile(installation);
                sID = readInstallationFile(installation);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return sID;
    }

    private static String readInstallationFile(File installation) throws IOException {
        RandomAccessFile f = new RandomAccessFile(installation, "r");
        byte[] bytes = new byte[(int) f.length()];
        f.readFully(bytes);
        f.close();
        return new String(bytes);
    }

    private static void writeInstallationFile(File installation) throws IOException {
        FileOutputStream out = new FileOutputStream(installation);
        String id = UUID.randomUUID().toString();
        out.write(id.getBytes());
        out.close();
    }
}
like image 109
Kevin Parker Avatar answered Oct 20 '22 12:10

Kevin Parker


((TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();

with manifest

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

Edit:

Here is some interesting reading about the android id:

How to set the Android ID

Android ID Requires Market Login

Try setting it to something other than 'android id' and see if you read the new value.

like image 25
drawnonward Avatar answered Oct 20 '22 14:10

drawnonward


Here is code segment how to get androidId, unique DeviceId and Serial Number for your android phone may be it helps you.

TelephonyManager tm = (TelephonyManager)getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
           final String DeviceId, SerialNum, androidId;
            DeviceId = tm.getDeviceId();
            SerialNum = tm.getSimSerialNumber();
            androidId = Secure.getString(getContentResolver(),Secure.ANDROID_ID);

            UUID deviceUuid = new UUID(androidId.hashCode(), ((long)DeviceId.hashCode() << 32) | SerialNum.hashCode());
            String mydeviceId = deviceUuid.toString();
            Log.v("My Id", "Android DeviceId is: " +DeviceId); 
            Log.v("My Id", "Android SerialNum is: " +SerialNum); 
            Log.v("My Id", "Android androidId is: " +androidId);  
like image 32
naeemgik Avatar answered Oct 20 '22 14:10

naeemgik


Wireless MAC address is more unique than IMEI, because the later gets spoofed on stolen devices. Drawback is that it only works on WiFi enabled devices. WifiInfo

like image 41
ognian Avatar answered Oct 20 '22 14:10

ognian