Get Device ID Example In Android

Description:
This example will show how you can get the device id on which this app will be running in android.

Algorithm:

Create a new project by File-> New -> Android Project name it GetDeviceIDExample.

Write following into main.xml:

 

<LinearLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".GetDeviceIDExampleActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Device's ID is: " />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/deviceid" android:textColor="#BB0000"/>

</LinearLayout>

 

Run for output.

Steps:

1.) Create a project named GetDeviceIDExample and set the information as stated in the image.

Build Target: Android 4.2
Application Name: GetDeviceIDExample
Package Name: com.example.GetDeviceIDExample
Activity Name: GetDeviceIDExampleActivity
Min SDK Version: 2.2

2.) Open GetDeviceIDExampleActivity.java file and write following code there:

 

package com.example.getdeviceidexample;

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

public class GetDeviceIDExampleActivity extends Activity {

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

String id = Secure.getString(getContentResolver(), Secure.ANDROID_ID);
TextView tv = (TextView) findViewById(R.id.deviceid);
tv.setText(id);

}

}

 

Compile and build the project.

Output

Leave a Comment:

3 comments
Mobile application development courses says August 16, 2013

Pretty element of content. I simply stumbled upon your website and in accession capital to assert that I acquire in fact enjoyed account your blog posts. Any way I will be subscribing on your augment and even I achievement you get right of entry to constantly quickly.

Reply
Andy Res says October 3, 2013

Thanks for the example.

Reply
AAMADI.COM says October 31, 2013

Hi,
First i like to thank you for those nice post. I like to learn about android development. But i can understand how can i do in my way. I use ADT. I like to make a apps for http://www.aamadi.com. When the apps is going to open they show a loading image and progress bar. When the apps will start they show some button as like page1, page2, page3 etc. When user click a button apps show a web page, like http://www.aamadi.com/index or http://www.aamadi.com/sound. And if the device failed to connect the internet, they show a error msg or image. Can you give me some guide line for this. I will very glad about that. Send me information about this to shanjedul@yahoo.com
Thanks in advance.

Reply
Add Your Reply