电话选择器API用于检测电话中正在使用的电话号码。使用此功能,可以避免用户手动输入电话号码,并提示他们选择所需的号码。下面给出了一个示例图像,以使您对本文中要做的事情有一个了解。请注意,我们将使用Kotlin语言实施此项目。
分步实施
步骤1:创建一个新项目
要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Kotlin作为编程语言
第2步:将依赖项添加到build.gradle文件,然后单击“立即同步”
implementation “com.google.android.gms:play-services-auth:19.0.0”
步骤3:使用activity_main.xml文件
转到activity_main.xml文件,并参考以下代码。为简单起见,我们仅使用TextView在选择后显示数字。以下是activity_main.xml文件的代码。
XML
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();
}
}
}
步骤4:在MainActivity.kt内编写以下代码
- 要检索电话号码提示,首先,通过创建一个HintRequest对象来配置提示选择器对话框。然后,将HintRequest对象传递给certificateClient.getHintPickerIntent ()以获得提示用户选择电话号码的意图。最后,使用startIntentSenderForResult()启动意图。
- onActivityResult()方法将帮助获取用户选择的号码,然后您可以编写下一个逻辑以继续您的应用。
科特林
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();
}
}
}
输出:
Github链接: https://github.com/introidx/phone-Selecter
想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处前往由我们的专家精心策划的指南,以使您立即做好行业准备!