Update: Christopher Bull's answer get rids of deprecated calls. Neverthe less the real problem is still the same. (Api levels my devices have are 24 and 27)
Both of my phone's are working fine(receiving calls etc), I really don't understand how it cannot find a phone number. Also the slight loading animation which I cannot control really discourages me to use this api. Are there any alternatives (that don't require permissions). I am also scared of getting PHONE_READ_STATE permission cause my app isn't a messaging app and I don't want it to get rejected, can this happen?
I am trying to get the phone number of user in my android app without requesting any permission. I saw HintPickerIntent is used for this and decided to implement the functionality (with tons of deprecated functions).
Strange thing is that I tried this with 2 different real phones with SIM cards. Both of them one had result code 1002 and displayed a progress bar that disappeared quickly without opening any popup.
But one of them had a completely empty Intent
with action = android.intent.action.MAIN
mLaunchParams = MultiScreenLaunchParams
Here is my code(All of them run in activity but I can move it to fragment if it is the problem which I really doubt..)
googleApiClient = GoogleApiClient.Builder(this)
.addApi(Auth.CREDENTIALS_API)
.addConnectionCallbacks((this@MainActivity as GoogleApiClient.ConnectionCallbacks))
.addOnConnectionFailedListener((this@MainActivity as GoogleApiClient.OnConnectionFailedListener))
.build()
googleApiClient.connect() //Runs in onCreate
override fun onConnected(p0: Bundle?) {
val hintRequest = HintRequest.Builder()
.setPhoneNumberIdentifierSupported(true)
.build()
val intent = Auth.CredentialsApi.getHintPickerIntent(
googleApiClient, hintRequest
)
startIntentSenderForResult(
intent.intentSender,
500, null, 0, 0, 0,Bundle()
)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 500) {
if (resultCode == Activity.RESULT_OK) {
val credential: Credential? = data!!.getParcelableExtra(Credential.EXTRA_KEY)
val id = credential?.getId()// will need to process phone number string
}
}
}
//build.gradle
api 'com.google.android.gms:play-services-auth:19.0.0'
api 'com.google.android.gms:play-services-auth-api-phone:17.5.0'
implementation 'com.google.android.gms:play-services-identity:17.0.0'
implementation 'com.google.android.gms:play-services-base:17.0.0'
This is a new project so I have nothing else in build.gradle related to google-play-services. Problem may be as easy as not applying a plugin.
Also a related issue in tracker
Both of the devices I use are Samsung. If this is not fixable, is there any other way to retrieve phone number without permission? And can I release my application if I get phone state permission but my application is not mainly a messaging application?