This example explains how to get device unique id in android. The device unique id is needed when we want user registration for a specific device. This way we can achieve security. Most of the banking applications today are using such approach.

Android Activity Layout (activity_main.xml)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textColor="#cc0000"
        android:textSize="32sp" />

</RelativeLayout>

Android Activity (MainActivity.java)

package com.javatechig.getdeviceid;

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

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		//getting unique id for device
		String id = Secure.getString(getContentResolver(), Secure.ANDROID_ID);

		//displaying id in textview
		TextView tv = (TextView) findViewById(R.id.textView1);
		tv.setText(id);

	}

}

Output

Get Device ID Example in Android

Nilanchala

A blogger, a bit of tech freak and a software developer. He is a thought leader in the fusion of design and mobile technologies. He is the author of Xamarin Mobile Application Development for Android Book (goo.gl/qUZ0XV3), DZone MVB and founder of stacktips.com.

Related Articles

Join The Discussion

Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.
  • vinod

    it works fine!!!!!!!!!Thank you…..

  • Amr Essam

    that doesn’t work