how to check entered mobile number is exist in device or not?

Viewed 50

I have implemented Phone number selector API in my project, now I want to check if entered umber is exist in mobile or not? it should proceed only when the device has that entered number. And also number should be inserted in first slot. How to do that?

Here is my code:

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

PendingIntent intent = Credentials.getClient(Register.this).getHintPickerIntent(hintRequest);

try {
    startIntentSenderForResult(intent.getIntentSender(),CREDENTIAL_REQUEST,null,0,0,0,new Bundle());

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

enterNumber = findViewById(R.id.renter_number);
rback_btn = findViewById(R.id.rback_btn);
rback_btn.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View view)
    {
        Intent i = new Intent(getApplicationContext(), Welcome.class);
        startActivity(i);
    }
});

next = findViewById(R.id.nextbtn_r);
next.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        strnumber = enterNumber.getText().toString();
        String otp= new DecimalFormat("000000").format(new Random().nextInt(999999));
        Intent intent = new Intent(getApplicationContext(), Verification.class);
        intent.putExtra("Your OTP is:",otp);
        intent.putExtra("Number:",strnumber);
        startActivity(intent);
    }
});

@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == CREDENTIAL_REQUEST && resultCode == RESULT_OK) {
        //obtain the phone number from the result
        Credential credential = data.getParcelableExtra(Credential.EXTRA_KEY);
        enterNumber.setText(credential.getId().substring(3));

        Toast
            .makeText(context, "Mob"+credential.getId().substring(3), Toast.LENGTH_SHORT)
            .show();
    } else if (
            requestCode == CREDENTIAL_REQUEST 
            && requestCode == CredentialsApi.ACTIVITY_RESULT_NO_HINTS_AVAILABLE
        ) {
        Toast
            .makeText(context, "No Phone Numbers found!", Toast.LENGTH_SHORT)
            .show();
    }
}
1 Answers

getting phone number isn't possible on Android. there is a method:

TelephonyManager mgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String phoneNumber = mgr.getLine1Number();

that should work only with permission:

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

but thats not reliable with nowadays SIM cards... in fact most of devices will return null. you can check this in system Settings -> About -> Phone Identinty (or Phone number)

note that SIM card itself does have some identity params, like serial number, but it is NOT storing own number, it is attached to SIM card on network side. thats why most of phone number validation services are asking for entering phone number, then sending SMS and asking for re-writting code (or you can do it programmatically - after requesting validating SMS sending just check incoming SMSes in next few secs/mins, and you will need a permission for that)

Related