How can I programmatically get the phone number of the device that is running my android app?
Add this dependency:
implementation 'com.google.android.gms:play-services-auth:18.0.0'
To fetch phone number list use this:
val hintRequest = HintRequest.Builder()
.setPhoneNumberIdentifierSupported(true)
.build()
val intent = Credentials.getClient(context).getHintPickerIntent(hintRequest)
startIntentSenderForResult(
intent.intentSender,
PHONE_NUMBER_FETCH_REQUEST_CODE,
null,
0,
0,
0,
null
)
After tap on play services dialog:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent? {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == PHONE_NUMBER_FETCH_REQUEST_CODE) {
data?.getParcelableExtra(Credential.EXTRA_KEY)?.id?.let {
useFetchedPhoneNumber(it)
}
}
}