Get Device Imei ,Android ID, Dual Sim number

  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  

How to get both imei no of dual sim phone tutorial

In This Tutorial, we will see a tutorial on how to get the Device IMEI, Android Id, and Number of both Sim. Every phone has a unique IMEI no. so developers take IMEI and use this as an identifier of a device or a user, and also to validate the user.
Device IMEI and Android ID this are two different things which we can use as per requirement.

Imei(International Mobile Equipment Identity):- IMEI numbers of cellular phones connected to a GSM network are stored in a database (EIR – Equipment Identity Register) containing all valid mobile phone equipment.

AndroidID:- is 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.

So, Lets Start,

1. Create a new Project by Going to File ⇒ New Android Project. Fill all the Details and Name your Activity as MainActivity.

2. Once your Project is created successfully open your activity_main.xml file and Add Below Code.

<?xml version="1.0" encoding="utf-8"?>  
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:app="http://schemas.android.com/apk/res-auto"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   tools:context="com.targetandroid.info.getimeianduniqueid.MainActivity">  
   
   <TextView  
     android:id="@+id/tv1"  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:text="IMEI:-"  
     android:layout_marginTop="20dp"  
     android:gravity="center"/>  
   
   <TextView  
     android:id="@+id/tv_imei"  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:text="54965214368213"  
     android:textSize="30sp"  
     android:layout_below="@+id/tv1"  
     android:layout_marginTop="20dp"  
     android:gravity="center"/>  
   
   <TextView  
     android:id="@+id/tv2"  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:text="Android ID:-"  
     android:layout_below="@+id/tv_imei"  
     android:layout_marginTop="20dp"  
     android:gravity="center"/>  
   
   <TextView  
     android:id="@+id/tv_aid"  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:text="54965214368213"  
     android:textSize="30sp"  
     android:layout_below="@+id/tv2"  
     android:layout_marginTop="20dp"  
     android:gravity="center"/>  
   
   <Button  
     android:id="@+id/bt_getall"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_below="@+id/tv_aid"  
     android:layout_marginTop="20dp"  
     android:layout_centerInParent="true"  
     android:text="get all"/>  
   
   
 </RelativeLayout>  

3. Add a PhoneStat Permission to AndroidManifest.xml because we are asking for phone identity so to add this permission is Mandatory.

type the below line Above Application Tag to ask permission.

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

In our MainActivity, we made three different methods for all the three different value we want

getImeiNumber()
getClientPhoneNumber()
getAndroidId()

Telephony Manager is used to get the IMEI No.
Subscription Manager is used to get the Sim no.
Setting.Secure is used to get an Android ID.

Let’s see every method separately.

To Get IMEI No. of Device:- getImeiNumber()

private String getImeiNumber() {  
     final TelephonyManager telephonyManager= (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);  
   
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {  
       //getDeviceId() is Deprecated so for android O we can use getImei() method  
       return telephonyManager.getImei();  
     }  
     else {  
       return telephonyManager.getDeviceId();  
     }  
   
   }  

To Get Dual Sim no of Client:- getClientPhoneNumber()

private void getClientPhoneNumber() {  
     try{  
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {  
         subInfoList = subscriptionManager.getActiveSubscriptionInfoList();  
       }  
       if (subInfoList.size() > 1)  
       {  
         isMultiSimEnabled = true;  
       }  
       for (SubscriptionInfo subscriptionInfo : subInfoList)  
   
       {  
   
           numbers.add(subscriptionInfo.getNumber());  
   
       }  
       Log.e(TAG,"Sim 1:- "+numbers.get(0));  
       Log.e(TAG,"Sim 2:- "+ numbers.get(1));  
     }catch (Exception e)  
     {  
       Log.d(TAG,e.toString());  
     }  
   
   
   }  

To Get AndroidId of Device:- getAndroidId()

private String getAndroidId() {  
   
     androidid = Settings.Secure.getString(getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);  
   
     Log.e("TAG",Settings.Secure.getString(getApplicationContext().getContentResolver(), Settings.Secure.ALLOWED_GEOLOCATION_ORIGINS));  
     Log.e("TAG",Settings.Secure.getString(getApplicationContext().getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD));  
   
     return androidid;  
   }

4. final code of MainActivity.java will be like

import android.Manifest;  
 import android.content.Context;  
 import android.content.pm.PackageManager;  
 import android.os.Build;  
 import android.provider.Settings;  
 import android.support.annotation.NonNull;  
 import android.support.v4.app.ActivityCompat;  
 import android.support.v4.content.ContextCompat;  
 import android.support.v7.app.AppCompatActivity;  
 import android.os.Bundle;  
 import android.telephony.SubscriptionInfo;  
 import android.telephony.SubscriptionManager;  
 import android.telephony.TelephonyManager;  
 import android.util.Log;  
 import android.view.View;  
 import android.widget.Button;  
 import android.widget.TextView;  
 import android.widget.Toast;  
   
 import java.util.ArrayList;  
 import java.util.List;  
   
 public class MainActivity extends AppCompatActivity {  
   
   
   TextView tv_imei,tv_aid;  
   Button bt_getall;  
   String imei,androidid;  
   public static boolean isMultiSimEnabled = false;  
   public static List<SubscriptionInfo> subInfoList;  
   public static ArrayList<String> numbers;  
   private SubscriptionManager subscriptionManager;  
   static final Integer PHONESTATS = 0x1;  
   private final String TAG=MainActivity.class.getSimpleName();  
   
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
   
     tv_imei=(TextView)findViewById(R.id.tv_imei);  
     tv_aid=(TextView)findViewById(R.id.tv_aid);  
     bt_getall=(Button)findViewById(R.id.bt_getall);  
   
   
     numbers = new ArrayList<String>();  
     subscriptionManager = SubscriptionManager.from(MainActivity.this);  
   
     bt_getall.setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View view) {  
   
         askForPermission(Manifest.permission.READ_PHONE_STATE, PHONESTATS);  
   
         tv_imei.setText(imei);  
         tv_aid.setText(androidid);  
       }  
     });  
   
   
   }  
   
   
   private void askForPermission(String permission, Integer requestCode) {  
     if (ContextCompat.checkSelfPermission(MainActivity.this, permission) != PackageManager.PERMISSION_GRANTED) {  
   
       // Should show an explanation  
       if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, permission)) {  
   
         ActivityCompat.requestPermissions(MainActivity.this, new String[]{permission}, requestCode);  
   
       } else {  
   
         ActivityCompat.requestPermissions(MainActivity.this, new String[]{permission}, requestCode);  
       }  
     } else {  
       imei = getImeiNumber();  
       getClientPhoneNumber();  
       androidid=getAndroidId();  
       Toast.makeText(this,permission + " is already granted.", Toast.LENGTH_SHORT).show();  
     }  
   }  
   
   
   
   @Override  
   public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)  
   {  
     switch (requestCode) {  
       case 1: {  
   
         // If request is cancelled, the result arrays are empty.  
         if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {  
   
           imei = getImeiNumber();  
           getClientPhoneNumber();  
           androidid=getAndroidId();  
   
         } else {  
   
           Toast.makeText(MainActivity.this, "You have Denied the Permission", Toast.LENGTH_SHORT).show();  
         }  
         return;  
       }  
     }  
   }  
   
   
   private String getImeiNumber() {  
     final TelephonyManager telephonyManager= (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);  
   
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {  
       //getDeviceId() is Deprecated so for android O we can use getImei() method  
       return telephonyManager.getImei();  
     }  
     else {  
       return telephonyManager.getDeviceId();  
     }  
   
   }  
   
   private void getClientPhoneNumber() {  
     try{  
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {  
         subInfoList = subscriptionManager.getActiveSubscriptionInfoList();  
       }  
       //check whether the phone is of Multi sim or Not  
       if (subInfoList.size() > 1)  
       {  
         isMultiSimEnabled = true;  
       }  
       for (SubscriptionInfo subscriptionInfo : subInfoList)  
         //add all sim number into arraylist  
       {  
           numbers.add(subscriptionInfo.getNumber());  
       }  
       Log.e(TAG,"Sim 1:- "+numbers.get(0));  
       Log.e(TAG,"Sim 2:- "+ numbers.get(1));  
     }catch (Exception e)  
     {  
       Log.d(TAG,e.toString());  
     }  
   
   
   }  
   
   private String getAndroidId() {  
   
     androidid = Settings.Secure.getString(getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);  
   
     Log.e("TAG",Settings.Secure.getString(getApplicationContext().getContentResolver(), Settings.Secure.ALLOWED_GEOLOCATION_ORIGINS));  
     Log.e("TAG",Settings.Secure.getString(getApplicationContext().getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD));  
   
     return androidid;  
   }  
   
   
 }  

5. So All done Here Run your Project and See Output


Final output of how to get imei no. and device id tutorial
Final Output

  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  

3 Comments

  1. ihaxgamez flappy golf 2 November 16, 2017 Reply
  2. terrarium tv on linux November 17, 2017 Reply
  3. robindidomenico November 26, 2018 Reply

Add a Comment

Your email address will not be published. Required fields are marked *