How to Get the Unique ID of an Android Device?

There are different types of unique ID in a mobile device. We can get the device id ,IMEI id and many others.We will take a look  at how to get the unique Id of an android device.





Step-By-Step Implementation



Step 1: Create a new project in Android or you can use an already created project.  


                                                                                   or


Step 2: Create an XML file named Acitivity_main.xml in layout folder. 


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

 <RelativeLayout

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/idRLContainer"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".MainActivity">

 

  <TextView

     android:layout_width="match_parent"

     android:layout_height="wrap_content"

     android:layout_above="@id/idTVUniqueID"

     android:layout_margin="15dp"

     android:text="Uniqie ID in Android"

     android:textAlignment="center"

     android:textColor="@color/black"

     android:textSize="20sp"

     android:textStyle="bold" />

 

 <TextView

     android:id="@+id/idTVUniqueID"

     android:layout_width="match_parent"

     android:layout_height="wrap_content"

     android:layout_centerInParent="true"

     android:gravity="center"

     android:padding="4dp"

     android:textAlignment="center"

     android:textSize="20sp"

     android:textStyle="bold" />

 

</RelativeLayout>

Step 4: Create ActivityMain.java or java in your project inside (com.my application).

 import android.annotation.SuppressLint;

 import android.os.Bundle;

 import android.provider.Settings;

 import android.widget.TextView;

 import androidx.appcompat.app.AppCompatActivity; 

 public class MainActivity extends AppCompatActivity {

 private TextView uniqueIDTV;

 

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

uniqueIDTV = findViewById(R.id.idTVUniqueID);

@SuppressLint("HardwareIds")

String android_device_id = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
uniqueIDTV.setText(android_device_id);

}

 }

Step 5: Output of above example.



Happy coding!







365Bloggy May 17, 2024
Share this post
Tags
SUBSCRIBE THIS FORM


Archive