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
toREAD_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
- How to download and build Android 13, AOSP
- Notification permission request, pop up notification in Android 13
- Resolve `Access blocked: ComponentInfo` error on Android 13
- Fix error "android gradle plugin requires java 11 to run. you are currently using java 1.8."
- Android - Vibration, Vibrator, VibrationEffect example
- How to get phone number in Android
- How to create Bugreports and Logcat with ADB in Android
- Clear application user data with ADB in Android
- How to disable/enable apps with ADB in Android
- How to find PID of a package with adb in Android
- How to grant/revoke permissions with adb in Android
- How to install an apk(app) with adb in Android
- How to kill a process/package from ADB in Android
- How to transfer files using adb push/pull command in Android
- How to capture the screen from adb in Android
- How to uninstall/install system apps from adb in Android
- How to change Settings values from adb in Android
- How to Factory reset with ADB in Android
- How to get logcat from ADB command in Android
- How to capture Heap Dump from app with ADB in Android
- How to force stop an app with adb in Android
- How to start/stop a service from adb in Android
- How to send/broadcast an intent from adb command in Android
- How to start an activity from adb in Android
- How to generate input from adb in Android
- How to check SQLite DB table in Android Studio
- How to get memory info and thread list from adb in Android
- Android - Uri, Scheme and SSP(Scheme Specific Part)
- How to parse a xml file with XmlResourceParser in Android
- Android - Signing an apk with Platfom key
- How to fix "Cleartext HTTP ... not permitted" for Android development
- How to fix "kvm permission denied" for Android Emulator
- Using ADB over Wifi for Android development