How to get the mobile number of my device programmatically?

Huzaifa Asif :

I have tried using 2 methods for retrieving my phone number but both of them don't work. I used:

  1. TelephonyManager
  2. SubscriptionManager

I do get Network name, Country iso, and IMEI but whenever I try to return Number it returns nothing.

I have also added all the required permissions for these! My manifest looks like:

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

Code using TelephonyManager:

TelephonyManager phoneMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
phoneMgr.getLine1Number()

Code using SubscriptionManager:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        List<SubscriptionInfo> subscription = SubscriptionManager.from(getApplicationContext()).getActiveSubscriptionInfoList();
        for (int i = 0; i < subscription.size(); i++) {
            SubscriptionInfo info = subscription.get(i);
            Log.e("TAG", "number " + info.getNumber());
            Log.e("TAG", "network name : " + info.getCarrierName());
            Log.e("TAG", "country iso " + info.getCountryIso());
        }
    }

In both attempts I get nothing!

Is there any other way to get phone number or I'm doing something wrong?

Bhoomika Patel :

Nowadays the TelephonyManager does not help us. Play Services API without permission is good solution for this.

This dependency is useful for this

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

Now inside your Activity.java:

GoogleApiClient  mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(Auth.CREDENTIALS_API)
                .build();

        if (mGoogleApiClient != null) {
            mGoogleApiClient.connect();
        }

After this do request for Phone Number:

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

        PendingIntent intent = Auth.CredentialsApi.getHintPickerIntent(mGoogleApiClient, hintRequest);
        try {
            startIntentSenderForResult(intent.getIntentSender(), 1008, null, 0, 0, 0, null);
        } catch (IntentSender.SendIntentException e) {
            Log.e("", "Could not start hint picker Intent", e);
        }

Now you need to handle response in your onActivityResult like this:

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case 1008:
                if (resultCode == RESULT_OK) {
                    Credential cred = data.getParcelableExtra(Credential.EXTRA_KEY);
//                    cred.getId====: ====+919*******
                    Log.e("cred.getId", cred.getId());
                    userMob = cred.getId();


                } else {
                    // Sim Card not found!
                    Log.e("cred.getId", "1008 else");

                    return;
                }


                break;
        }
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=305369&siteId=1