[英]Telephonymanager.EXTRA_INCOMING_NUMBER is deprecated in API level 29
I am in situation where I need to identify incoming call phone number in Android but when using TelephonyManager.EXTRA_INCOMING_NUMBER android studio warning EXTRA_INCOMING_NUMBER is deprecated .I gone through the developers.android.com, it shows apps performing call screening should use the CallScreeningService API instead. I am in situation where I need to identify incoming call phone number in Android but when using TelephonyManager.EXTRA_INCOMING_NUMBER android studio warning EXTRA_INCOMING_NUMBER is deprecated .I gone through the developers.android.com, it shows apps performing call screening should use the CallScreeningService API instead . But I can't figure out how to use CallScreeningService to get incoming call phone number.但我不知道如何使用 CallScreeningService 来获取来电电话号码。 Anyone can help me?任何人都可以帮助我吗?
As @Saurabh said, the new way to screen calls is through the CallScreeningService
.正如@Saurabh 所说,筛选呼叫的新方法是通过CallScreeningService
。 However, for the service to work on Android Q and up, the user needs to set your app as the default caller ID & spam app (which is done by using the new RoleManager
class)但是,要让服务在 Android Q 及更高版本上运行,用户需要将您的应用程序设置为默认呼叫者 ID 和垃圾邮件应用程序(通过使用新的RoleManager
类完成)
Register your screening service:注册您的筛查服务:
<service android:name="com.example.ScreeningService" android:permission="android.permission.BIND_SCREENING_SERVICE"> <intent-filter> <action android:name="android.telecom.CallScreeningService"/> </intent-filter> </service>
Create you service class:为您创建服务 class:
@RequiresApi(api = Build.VERSION_CODES.N) class ScreeningService: CallScreeningService() { override fun onScreenCall(details: Details) { //code here } }
Request the screening role from the user in your main activity (or where ever you see as fit):在您的主要活动(或您认为合适的任何地方)中向用户请求筛选角色:
@RequiresApi(Build.VERSION_CODES.Q) private fun requestScreeningRole(){ val roleManager = getSystemService(Context.ROLE_SERVICE) as RoleManager val isHeld = roleManager.isRoleHeld(RoleManager.ROLE_CALL_SCREENING) if(.isHeld){ //ask the user to set your app as the default screening app val intent = roleManager.createRequestRoleIntent(RoleManager,ROLE_CALL_SCREENING) startActivityForResult(intent, 123) } else { //you are already the default screening app! } }
Catch the user's response:捕捉用户的响应:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) when (requestCode) { 123 -> { if (resultCode == Activity.RESULT_OK) { //The user set you as the default screening app. } else { //the user didn't set you as the default screening app... } } else -> {} } }
Apologies for using a hard coded request code >.<抱歉使用硬编码的请求代码>.<
Create a CallScreeningService
like this:像这样创建一个CallScreeningService
:
class ScreeningService : CallScreeningService() {
override fun onScreenCall(callDetails: Call.Details) {
val phoneNumber = callDetails.handle.schemeSpecificPart
// Do stuff with phone number
}
}
And register this service in your AndroidManifest.xml
:并在您的AndroidManifest.xml
中注册此服务:
<service android:name="your.package.ScreeningService"
android:permission="android.permission.BIND_SCREENING_SERVICE">
<intent-filter>
<action android:name="android.telecom.CallScreeningService"/>
</intent-filter>
</service>
I know it's pretty late but if someone has simillar problem I found a solution that works on API 28+ for TelephonyManager.我知道现在已经很晚了,但是如果有人有类似的问题,我找到了一个适用于 TelephonyManager 的 API 28+ 的解决方案。 For just identifing phone number CallScreeningService is an overkill, and you could probably override users private settings or another app that blocks spam calls.仅仅识别电话号码 CallScreeningService 是一种过度杀伤,您可能会覆盖用户的私人设置或阻止垃圾邮件呼叫的其他应用程序。
You need to add android.permission.READ_CALL_LOG in your manifest and request it in a runtime.您需要在清单中添加android.permission.READ_CALL_LOG并在运行时请求它。
if (Build.VERSION.SdkInt >= BuildVersionCodes.P) {
if (ApplicationContext.CheckSelfPermission(Manifest.Permission.ReadCallLog) != Permission.Granted)
{
ActivityCompat.RequestPermissions(this, new string[] { Manifest.Permission.ReadCallLog }, ACTION_READ_CALL_LOG);
}
}
Provided user allows your app to read call log, after broad cast receiver hits OnReceive => ActionPhoneStateChanged phone number will be empty at the first instance, but second time it should be populated.如果用户允许您的应用程序读取通话记录,则在广播接收器点击 OnReceive => ActionPhoneStateChanged 电话号码后,电话号码将在第一次为空,但第二次应填充。 So be prepared that on Api 28+ phone number could be identified at the second time.所以准备好在 Api 28+ 电话号码可以在第二次识别。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.