How to get phone number in Android

This article will introduce how to get phone number in Android.

1. TelephonyManager.getLine1Number() API

TelephonyManager.getLine1Number() returns the phone number.

You can implement it in Kotlin like this:

override fun onCreate(savedInstanceState: Bundle?) {
    ...

    val number = getPhoneNumber()
    Log.d("Test", "number: $number")
}

@SuppressLint("MissingPermission")
fun getPhoneNumber(): String {
    var tm = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
    return tm.line1Number
}

When the above code is executed, the phone number is output as follows.

10-26 23:21:31.171 22398 22398 D Test    : number: 01012349876

2. Permission settings

You must have Phone permission to use getLine1Number() API. Required permissions have changed in Android 11, and you must request permissions taking this into account.

When the Target SDK API is 30 (Android 11) or higher, you must request the READ_PHONE_NUMBERS permission in the Manifest. Of course, since it is a Runtime Permission, you must request permission from the user.

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

When the Target SDK API is less than 30 (Android 10 or lower), you must request the READ_PHONE_STATE permission in the Manifest. This permission is also a Runtime Permission.

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

In general, since apps work on both Android 10 and 11, both of the above permissions must be defined. To request READ_PHONE_NUMBERS permission only for Android 10 or lower, set Max SDK version as maxSdkVersion="29" as shown below.

<manifest>
    <!-- Grants the READ_PHONE_STATE permission only on devices that run
         Android 10 (API level 29) and lower. -->
    <uses-permission android:name="READ_PHONE_STATE"
                     android:maxSdkVersion="29" />
    <uses-permission android:name="READ_PHONE_NUMBERS" />
</manifest>

As one of the changes in Android 11, the permission required to get phone numbers has been changed from READ_PHONE_STATE to READ_PHONE_NUMBERS.

2.1 SMS permission

Typically, the READ_PHONE_NUMBERS permission is used, but if you look at the API documentation, you can get the phone number using getLine1Number() even if you have the getLine1Number() permission.

/**
 * Returns the phone number string for line 1, for example, the MSISDN
 * for a GSM phone for a particular subscription. Return null if it is unavailable.
 * <p>
 * The default SMS app can also use this.
 *
 * <p>Requires Permission:
 *     {@link android.Manifest.permission#READ_SMS READ_SMS},
 *     {@link android.Manifest.permission#READ_PHONE_NUMBERS READ_PHONE_NUMBERS},
 *     that the caller is the default SMS app,
 *     or that the caller has carrier privileges (see {@link #hasCarrierPrivileges})
 *     for any API level.
 *     {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
 *     for apps targeting SDK API level 29 and below.
 */
@RequiresPermission(anyOf = {
        android.Manifest.permission.READ_PHONE_STATE,
        android.Manifest.permission.READ_SMS,
        android.Manifest.permission.READ_PHONE_NUMBERS
})
public String getLine1Number() {
    return getLine1Number(getSubId());
}

2.2 The permission of "READ_PRIVILEGED_PHONE_STATE"

android.permission.READ_PRIVILEGED_PHONE_STATE is a permission granted to apps that are the same signer as Platform or to Privileged apps. Since this permission is only allowed to system apps, it is not introduced in the API documentation. Even if you have this permission, you can get the phone number with getLine1Number().

Since this permission is an Install permission, not a Runtime permission, there is no need to request permission from the user. However, as described above, general 3rd party apps cannot receive this permission.

Related Posts

codechachaCopyright ©2019 codechacha