How to get the phone number programmatically in Android

How to get the phone number programmatically in Android

In this tutorial, you will learn to get own phone number or application running device phone number programmatically in Android.

There are two ways to get phone number in Android

  • TelephonyManager (from API 1)
  • SubscriptionManager (from API 22)

TelephonyManager

Android TelephonyManager provides information about the android telephony system. To use the TelephonyManager first get the instance of the Telephony Service by calling Context.getSystemService(TELEPHONY_SERVICE). This telephony service can be used to retrieve call state, cell location, operator name, etc. As well as to listen to the various telephony events.

First of all, don't forget add READ_PHONE_STATE permission to AndroidManifest.xml.

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

Use below code

private String getPhone() {
    TelephonyManager phoneMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    if (ActivityCompat.checkSelfPermission(activity, wantPermission) != PackageManager.PERMISSION_GRANTED) {
        return "";
    }
    return phoneMgr.getLine1Number();
}

This method only works on devices where the number is stored on the SIM card, which only some carriers do. For all other carriers, you will have to ask the user to enter the phone number manually, as the number is simply not stored anywhere on the device from where you can retrieve it.

Full code

public class PhoneActivity extends AppCompatActivity {
    String TAG = "PhoneActivityTAG";
    Activity activity = PhoneActivity.this;
    String wantPermission = Manifest.permission.READ_PHONE_STATE;
    private static final int PERMISSION_REQUEST_CODE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone);

        if (!checkPermission(wantPermission)) {
            requestPermission(wantPermission);
        } else {
            Log.d(TAG, "Phone number: " + getPhone());
        }
    }

    private String getPhone() {
        TelephonyManager phoneMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        if (ActivityCompat.checkSelfPermission(activity, wantPermission) != PackageManager.PERMISSION_GRANTED) {
            return "";
        }
        return phoneMgr.getLine1Number();
    }

    private void requestPermission(String permission){
        if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)){
            Toast.makeText(activity, "Phone state permission allows us to get phone number. Please allow it for additional functionality.", Toast.LENGTH_LONG).show();
        }
        ActivityCompat.requestPermissions(activity, new String[]{permission},PERMISSION_REQUEST_CODE);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case PERMISSION_REQUEST_CODE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Log.d(TAG, "Phone number: " + getPhone());
                } else {
                    Toast.makeText(activity,"Permission Denied. We can't get phone number.", Toast.LENGTH_LONG).show();
                }
                break;
        }
    }

    private boolean checkPermission(String permission){
        if (Build.VERSION.SDK_INT >= 23) {
            int result = ContextCompat.checkSelfPermission(activity, permission);
            if (result == PackageManager.PERMISSION_GRANTED){
                return true;
            } else {
                return false;
            }
        } else {
            return true;
        }
    }
}

SubscriptionManager

Android 5.1 adds support for using more than one cellular carrier SIM card at a time. This feature lets users activate and use additional SIMs on devices that have two or more SIM card slots.

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.d(TAG, "number " + info.getNumber());
        Log.d(TAG, "network name : " + info.getCarrierName());
        Log.d(TAG, "country iso " + info.getCountryIso());
    }
}
comments powered by Disqus