How to Use Phone Selector API in Android?
Phone Selector API is used to detect phone numbers being used in the phone. Using this you can avoid manual input of Phone Number by users and prompt them to choose the desired number. A sample image is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Kotlin language.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language
Step 2: Add dependency to the build.gradle file and click “sync now”
implementation “com.google.android.gms:play-services-auth:19.0.0”
Step 3: Working with the activity_main.xml file
Go to the activity_main.xml file and refer to the following code. For simplicity, we are using just a TextView to show the number after selection. Below is the code for the activity_main.xml file.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < androidx.constraintlayout.widget.ConstraintLayout android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".MainActivity" > < TextView android:id = "@+id/tv1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "Hello World!" app:layout_constraintBottom_toBottomOf = "parent" app:layout_constraintLeft_toLeftOf = "parent" app:layout_constraintRight_toRightOf = "parent" app:layout_constraintTop_toTopOf = "parent" /> < Button android:id = "@+id/btn_open" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_marginTop = "30dp" android:text = "Open" app:layout_constraintLeft_toLeftOf = "@id/tv1" app:layout_constraintTop_toBottomOf = "@id/tv1" /> </ androidx.constraintlayout.widget.ConstraintLayout > |
Step 4: Inside MainActivity.kt write the following code
- To retrieve the Phone Number hints, first, configure the hint selector dialog by creating a HintRequest object. Then, pass the HintRequest object to credentialsClient.getHintPickerIntent() to get an intent to prompt the user to choose a phone number. Finally, start the intent with startIntentSenderForResult().
- onActivityResult() method will help to get the number user has selected and then you can write the next logic to continue with your app.
Kotlin
import android.content.Intent import android.content.IntentSender import android.os.Bundle import android.widget.Button import android.widget.TextView import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import com.google.android.gms.auth.api.credentials.* class MainActivity : AppCompatActivity() { lateinit var open_btn: Button lateinit var tv1: TextView companion object { var CREDENTIAL_PICKER_REQUEST = 1 } override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout.activity_main) open_btn = findViewById(R.id.btn_open) tv1 = findViewById(R.id.tv1) // set on click listener to button // to open the phone selector dialog open_btn.setOnClickListener { phoneSelection() } } private fun phoneSelection() { // To retrieve the Phone Number hints, first, configure // the hint selector dialog by creating a HintRequest object. val hintRequest = HintRequest.Builder() .setPhoneNumberIdentifierSupported( true ) .build() val options = CredentialsOptions.Builder() .forceEnableSaveDialog() .build() // Then, pass the HintRequest object to // credentialsClient.getHintPickerIntent() // to get an intent to prompt the user to // choose a phone number. val credentialsClient = Credentials.getClient(applicationContext, options) val intent = credentialsClient.getHintPickerIntent(hintRequest) try { startIntentSenderForResult( intent.intentSender, CREDENTIAL_PICKER_REQUEST, null , 0 , 0 , 0 , Bundle() ) } catch (e: IntentSender.SendIntentException) { e.printStackTrace() } } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super .onActivityResult(requestCode, resultCode, data) if (requestCode == CREDENTIAL_PICKER_REQUEST && resultCode == RESULT_OK) { // get data from the dialog which is of type Credential val credential: Credential? = data?.getParcelableExtra(Credential.EXTRA_KEY) // set the received data t the text view credential?.apply { tv1.text = credential.id } } else if (requestCode == CREDENTIAL_PICKER_REQUEST && resultCode == CredentialsApi.ACTIVITY_RESULT_NO_HINTS_AVAILABLE) { Toast.makeText( this , "No phone numbers found" , Toast.LENGTH_LONG).show(); } } } |
Output:
Github Link: https://github.com/introidx/phone-Selecter
Please Login to comment...