0

I am working on an application which requires that the user enters his phone number on registration, and that number should present on same device. Is there any way find it?

I've already checked with the TelephonyManager but it doesn't guarantee that always get the number:

private void getMobileNumber() {

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

}

I've already checked with the SubscriptionManager too, and it doesn't guarantee either:

private void GetCarriorsInformation() {

       ArrayList<string Numbers = new ArrayList<>();
       SubscriptionManager mSubscriptionManager = SubscriptionManager.from(context);
      List<SubscriptionInfo> subInfoList = mSubscriptionManager.getActiveSubscriptionInfoList();
        if (subInfoList.size() > 1) {
            isMultiSimEnabled = true;
        }
        for (SubscriptionInfo subscriptionInfo : subInfoList) {
            Numbers.add(subscriptionInfo.getNumber());
        }

  }

Is there any other way to validate if the phone number is present on same device or not?

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
Shiv Jalkote
  • 111
  • 6
  • See https://stackoverflow.com/a/23675998/8034839 – shizhen Jun 27 '19 at 05:37
  • As far as I know there isn't a reliable way to get the number from the device as you've already found out. But if all you need is to verify that the user has access to that phone number, a one time code sent via sms could work. https://developers.google.com/identity/sms-retriever/overview – Subaru Tashiro Jun 27 '19 at 05:43
  • @SubaruTashiro Google API gives number associate with the email account. You can validate number is correct and working but how do you know number is belongs the same device or not? – Shiv Jalkote Jun 27 '19 at 05:56

2 Answers2

0

You cannot get the phone number through code, but you can verify or validate the number is on the same device even by using Firebase Authentication method.

  PhoneAuthProvider.getInstance().verifyPhoneNumber(
                username,
                60,
                TimeUnit.SECONDS,
                RegisterActivity.this,
                new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
                    @Override
                    public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
                        Intent data = new Intent();
                        data.putExtra(StaticConfig.STR_EXTRA_USERNAME, phoneNumber);
                        data.putExtra(StaticConfig.STR_EXTRA_PASSWORD, userPassword);
                        data.putExtra(StaticConfig.STR_EXTRA_CREDENTIAL, phoneAuthCredential);
                        data.putExtra(StaticConfig.STR_EXTRA_ACTION, STR_EXTRA_ACTION_REGISTER);
                        setResult(RESULT_OK, data);
                        finish();
                    }

                    @Override
                    public void onVerificationFailed(FirebaseException e) {
                        avi.hide();
                        String exception = e.getMessage();
                        Toast.makeText(RegisterActivity.this, exception, Toast.LENGTH_SHORT).show();
                      showSnackBar("Kindly provide mobile number used in this phone");
                        editTextUserPhone.setEnabled(true);
                    }

                    @Override
                    public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
                        super.onCodeSent(s, forceResendingToken);
                        codeSent = s;
                    }
                }

on verificationStateChangedCallbacks look for the incoming messages, and if the number is in the same device the method is called.

follow this link for more detail and documentation for Firebase Authentication.

Manzoor Ahmad
  • 481
  • 6
  • 21
0

Not exactly, but one more way you can request the phone number using hint api. You dont need user to enter their number. They can directly choose the number available on device.

 private GoogleApiClient apiClient;
 private static final int RC_HINT = 1000;

private void requestHint() {
    apiClient = new GoogleApiClient.Builder(this)
            .addApi(Auth.CREDENTIALS_API)
            .build();

    CredentialPickerConfig conf = new CredentialPickerConfig.Builder()
            .setShowAddAccountButton(true)
            .build();

    HintRequest hintRequest = new HintRequest.Builder()
            .setPhoneNumberIdentifierSupported(true)
            .setHintPickerConfig(conf)
            .build();

    PendingIntent intent = Auth.CredentialsApi.getHintPickerIntent(
            apiClient, hintRequest);
    try {
        startIntentSenderForResult(intent.getIntentSender(),
                RC_HINT, null, 0, 0, 0);

    } catch (IntentSender.SendIntentException e) {
        e.printStackTrace();
    }
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RC_HINT && resultCode == RESULT_OK) {
        Credential cred = data.getParcelableExtra(Credential.EXTRA_KEY);
        Toast.makeText(this, "Number = " + cred.getId(), 
     Toast.LENGTH_LONG).show();
  }
}
SimpleCoder
  • 1,665
  • 1
  • 21
  • 34