0

In my App, i want to verify if the mobile number that the user has entered is present on current device. Like all the banking apps do. Banking apps will not allow you to login if registered mobile is not present on current device.

My question is how to do that. I have tried TelephonyManager API and SubscriptionManager API. Both of them are not giving the mobile number. With SubscriptionManager API, i'm able to get the following info, but not the mobile number

SubscriptionManager subscriptionManager = SubscriptionManager.from(getApplicationContext()); 
List subsInfoList = subscriptionManager.getActiveSubscriptionInfoList(); 
if (subsInfoList != null) { 
    Log.d("log", "number of sims = " + subsInfoList.size()); 
    Log.d("log", "Current list = " + subsInfoList); 
    for (SubscriptionInfo subscriptionInfo : subsInfoList) { 
        Log.d("log", " Number is " + subscriptionInfo.getNumber()); 
        tvNumber.setText(subscriptionInfo.getNumber()); 
    } 
} 

number of sims = 1

Current list = [{id=1, iccId=89914509009117870249 simSlotIndex=0 displayName=airtel carrierName=airtel nameSource=2 iconTint=-13408298 dataRoaming=1 iconBitmap=android.graphics.Bitmap@326dab88 mcc 404 mnc 45 status 1}] 
Number is

TelephonyManager tMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); String mobileNumber = tMgr.getLine1Number(); Log.d("log", "mobile number = " + mobileNumber);
Waqar UlHaq
  • 6,144
  • 2
  • 34
  • 42
Sakibul Haque
  • 87
  • 1
  • 2
  • 12

2 Answers2

2

Replace your last line with

TelephonyManager tMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();

Its important to ensure the following permission is enabled:

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

Note: This can return null, the empty string, or "???????"

Ref: Programmatically obtain the phone number of the Android phone

whoward3
  • 81
  • 4
0

If you send a One Time Passcode to the number you expect the device to be, you can ask them to enter that passcode to prove they have current access to that device.

You can use a hint picker for them to select the phone number:

// Construct a request for phone numbers and show the picker
private void requestHint() {
    HintRequest hintRequest = new HintRequest.Builder()
           .setPhoneNumberIdentifierSupported(true)
           .build();

    PendingIntent intent = Auth.CredentialsApi.getHintPickerIntent(
            apiClient, hintRequest);
    startIntentSenderForResult(intent.getIntentSender(),
            RESOLVE_HINT, null, 0, 0, 0);
}

// Obtain the phone number from the result
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  if (requestCode == RESOLVE_HINT) {
      if (resultCode == RESULT_OK) {
          Credential credential = data.getParcelableExtra(Credential.EXTRA_KEY);
          // credential.getId();  <-- will need to process phone number string
      }
  }
}

Note: this solution does not need any special AndroidManifest permissions

You may need this dependency:

implementation "com.google.android.gms:play-services-auth:16.0.1"

Refs:

https://developers.google.com/identity/sms-retriever/request

https://developers.google.com/identity/sms-retriever/overview

https://proandroiddev.com/no-more-sms-call-log-permissions-now-what-9b8226de7827?gi=4f2177b8016e

Blundell
  • 75,855
  • 30
  • 208
  • 233