Programmatically obtain the phone number of the Android phone

后端 未结 18 1703
南笙
南笙 2020-11-21 06:17

How can I programmatically get the phone number of the device that is running my android app?

18条回答
  •  情深已故
    2020-11-21 06:58

    Here's a combination of the solutions I've found (sample project here, if you want to also check auto-fill):

    manifest

        
    

    build.gradle

        implementation "com.google.android.gms:play-services-auth:17.0.0"
    

    MainActivity.kt

    class MainActivity : AppCompatActivity() {
        private lateinit var googleApiClient: GoogleApiClient
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            tryGetCurrentUserPhoneNumber(this)
            googleApiClient = GoogleApiClient.Builder(this).addApi(Auth.CREDENTIALS_API).build()
            if (phoneNumber.isEmpty()) {
                val hintRequest = HintRequest.Builder().setPhoneNumberIdentifierSupported(true).build()
                val intent = Auth.CredentialsApi.getHintPickerIntent(googleApiClient, hintRequest)
                try {
                    startIntentSenderForResult(intent.intentSender, REQUEST_PHONE_NUMBER, null, 0, 0, 0);
                } catch (e: IntentSender.SendIntentException) {
                    Toast.makeText(this, "failed to show phone picker", Toast.LENGTH_SHORT).show()
                }
            } else
                onGotPhoneNumberToSendTo()
    
        }
    
        override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
            super.onActivityResult(requestCode, resultCode, data)
            if (requestCode == REQUEST_PHONE_NUMBER) {
                if (resultCode == Activity.RESULT_OK) {
                    val cred: Credential? = data?.getParcelableExtra(Credential.EXTRA_KEY)
                    phoneNumber = cred?.id ?: ""
                    if (phoneNumber.isEmpty())
                        Toast.makeText(this, "failed to get phone number", Toast.LENGTH_SHORT).show()
                    else
                        onGotPhoneNumberToSendTo()
                }
            }
        }
    
        private fun onGotPhoneNumberToSendTo() {
            Toast.makeText(this, "got number:$phoneNumber", Toast.LENGTH_SHORT).show()
        }
    
    
        companion object {
            private const val REQUEST_PHONE_NUMBER = 1
            private var phoneNumber = ""
    
            @SuppressLint("MissingPermission", "HardwareIds")
            private fun tryGetCurrentUserPhoneNumber(context: Context): String {
                if (phoneNumber.isNotEmpty())
                    return phoneNumber
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    val subscriptionManager = context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE) as SubscriptionManager
                    try {
                        subscriptionManager.activeSubscriptionInfoList?.forEach {
                            val number: String? = it.number
                            if (!number.isNullOrBlank()) {
                                phoneNumber = number
                                return number
                            }
                        }
                    } catch (ignored: Exception) {
                    }
                }
                try {
                    val telephonyManager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
                    val number = telephonyManager.line1Number ?: ""
                    if (!number.isBlank()) {
                        phoneNumber = number
                        return number
                    }
                } catch (e: Exception) {
                }
                return ""
            }
        }
    }
    

提交回复
热议问题