One of a kind – getting the unique id which identifies the Android device

Each Android devices allow to read the device settings via the class “Secure”. Especially for C2DM you have a need to uniquely identify the Android device so that you do not register the device several times for Cloud-to-device-messaging. From the Javadoc of “android.provider.Settings.Secure.ANDROID_ID”:

A 64-bit number (as a hex string) that is randomly generated on the device’s first boot and should remain constant for the lifetime of the device. (The value may change if a factory reset is performed on the device.)

Here is a little Activity which read this device identifier and shows it as a Toast.

package de.vogella.android.deviceinfo;

import android.app.Activity;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.widget.Toast;

public class ShowDeviceInfo extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		String deviceId = Secure.getString(this.getContentResolver(),
				Secure.ANDROID_ID);
		Toast.makeText(this, deviceId, Toast.LENGTH_SHORT).show();
	}
}

This way you can make sure that certain thing a uniquely defined for your device.

About Lars Vogel

Lars Vogel is the founder and CEO of the vogella GmbH and works as Eclipse and Android consultant, trainer and book author. He is a regular speaker at international conferences, With more than one million visitors per month his website vogella.com is one of the central sources for Eclipse and Android programming information.
This entry was posted in Android. Bookmark the permalink.

2 Responses to One of a kind – getting the unique id which identifies the Android device

  1. Mark Murphy says:

    Bear in mind that ANDROID_ID can be modified by owners of rooted devices, and that device manufacturers sometimes screw up (e.g., ANDROID_ID not being unique, ANDROID_ID not being hex). See http://android-developers.blogspot.com/2011/03/identifying-app-installations.html for more.

  2. Lars Vogel says:

    @Mark: Thanks for the clarification.

Comments are closed.