Is there anyway I can get mobile number of both SIM cards in android Java?

788 Views Asked by At

I want to add the functionality of login similar to UPI apps. The user will select the SIM card from his phone and I will send an SMS from that SIM and will verify his phone number using that SMS. But I have to compare the number in the back-end or in API. So, I want to acquire the phone numbers of both SIM cards or the phone number of the SIM card user selects (Get the phone number from SMS or while sending SMS).

I am putting a generic class that I made for SIM selections and SMS. I have tried getting the mobile number from SubscriptionInfo, but it gives the operator name and all details right except the mobile number which returns an empty string. I have found in different blogs or my R&D that it is up to the operator whether to put the number there or not. So, if I am getting the mobile number, it is not 100% guaranteed that I will get the mobile number every time.

Here's my generic class: SimUtil.java

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.telephony.SmsManager;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class SimUtil {

    List<SimCard> simCards = new ArrayList<>();
    Context context;

    public SimUtil(Context context) {
        this.context = context;
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            setupPermissions();
            return;
        }
        List<SubscriptionInfo> subscriptionInfos = SubscriptionManager.from(context.getApplicationContext()).getActiveSubscriptionInfoList();
        for (int i = 0; i < subscriptionInfos.size(); i++) {
            SubscriptionInfo currentCard = subscriptionInfos.get(i);
            SimCard simCard = new SimCard(i,currentCard);
            simCards.add(simCard);
        }
    }

    public int getSimCardCount() {
        return this.simCards.size();
    }

    public List<SimCard> getSimCardList() {
        if(simCards.size() > 0)
            return simCards;
        else
            return null;
    }

    public SimCard getFirstSimCard() {
        return simCards.get(0);
    }

    public boolean hasDualSim() {
        return simCards.size() > 1;
    }

    public SimCard getSecondSimCard() {
        if(hasDualSim())
            return simCards.get(1);
        else
            return null;
    }

    public static void sendSMSFromSim1(String message) {
        SmsManager.getSmsManagerForSubscriptionId(0)
                .sendTextMessage(AppConstant.LOGIN_MOBILE_NUMBER, null, message, null, null);
    }

    public static void sendSMSFromSim2(String message) {
        SmsManager.getSmsManagerForSubscriptionId(1)
                .sendTextMessage(AppConstant.LOGIN_MOBILE_NUMBER, null, message, null, null);
    }

    private void setupPermissions() {
        int permission = ContextCompat.checkSelfPermission(context, Manifest.permission.SEND_SMS);
        if(permission != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions((Activity)context, new String[]{Manifest.permission.SEND_SMS}, 1);
        }

        int permission2 = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE);
        if(permission2 != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions((Activity)context, new String[]{Manifest.permission.READ_PHONE_STATE}, 1);
        }
    }

    public static class SimCard {
        int index;
        String operatorName;
        String mobileNumber;
        String countryISO;

        public SimCard(int index, String operatorName, String countryISO, String mobileNumber) {
            this.index = index;
            this.operatorName = operatorName;
            this.countryISO = countryISO;
            this.mobileNumber = mobileNumber;
        }

        public SimCard(int index,SubscriptionInfo subscriptionInfo) {
            this.index = index;
            this.operatorName = subscriptionInfo.getCarrierName().toString();
            this.countryISO = subscriptionInfo.getCountryIso();
            this.mobileNumber = subscriptionInfo.getNumber();
        }

        public int getIndex() {
            return index;
        }

        public String getOperatorName() {
            return operatorName;
        }

        public String getCountryISO() {
            return countryISO;
        }

        public String getMobileNumber() {
            return mobileNumber;
        }
    }
}
0

There are 0 best solutions below