Monday, November 23, 2015

Android deviceid | Device id of Android | Finding device id in Android

Hello Friends,
Today am going to post a logical code to find the device id of an Android device. Android Device ID is the specific alpha-numeric Identification code associated with your mobile device.

Download Code

1) AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.codehelp4you.deviceidfinder"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".HomeActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


2) HomeActivity.java


/*
 * Created by Sathishkumar Durai
 * Code Help 4 You
 * Write to: sathish.codehelp4you@gmail.com
 */

package com.codehelp4you.deviceidfinder;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;

/***
 * 
 * @author Sathish
 *
 */

public class HomeActivity extends Activity {

 private Button getButton;
 
 private Context context = this;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_home);
 }
 
 @Override
 protected void onResume() {
  super.onResume();
  
  getButton = (Button)findViewById(R.id.getId);
  getButton.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    AlertDialog alertDialog = new AlertDialog.Builder(context).create();
    alertDialog.setTitle("Device ID");
    alertDialog.setMessage(GetDeviceID(context));
    alertDialog.setButton(DialogInterface.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int which) {
        dialog.dismiss();
       }
    });
    
    alertDialog.show();
   }
  });
 }
 
 /**
  * This function return the device ID given the context
  * @param context
  * @return
  */
 public String GetDeviceID(Context context)
 {
  return Settings.Secure.getString(context.getContentResolver(),Settings.Secure.ANDROID_ID); 
 }

}


3) activity_home.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="fill_parent" 
    android:layout_gravity="center">

    <Button
        android:id="@+id/getId"
        android:text="Get ID"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:paddingLeft="30dp"
        android:paddingRight="30dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="20sp" />

</RelativeLayout>


4) string.xml


<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">DeviceIDDemo</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>

</resources>

Thank you and See you in the next tutorial!