简体   繁体   English

Telephonymanager.EXTRA_INCOMING_NUMBER 在 API 级别 29 中已弃用

[英]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类完成)

  1. 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>
  2. Create you service class:为您创建服务 class:

     @RequiresApi(api = Build.VERSION_CODES.N) class ScreeningService: CallScreeningService() { override fun onScreenCall(details: Details) { //code here } }
  3. 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! } }
  4. 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.

相关问题 未指定大小的 Android ThumbnailUtils.createVideoThumbnail 在 API 级别 29 中已弃用 - Android ThumbnailUtils.createVideoThumbnail with no Size specified is deprecated in API level 29 对于 API &gt; 29 和 API &lt; 29 对于 Android 应用程序,我需要获取来电电话号码和该号码的通话记录的最低权限是多少? - What are the minimum permissions I need to get incoming phone number and call history for that number for API > 29 and API < 29 for Android app? 如何在 Android SDK API 29 级或更高级别中获取 IMSI 编号? - How to get IMSI Number in Android SDK API Level 29 or higher? 在API 29 java android中不推荐使用getBitmap - getBitmap deprecated in API 29 java android 文件输入 Stream 在 Api 级别 29 上不起作用 - File Input Stream not working on Api level 29 GPS 在 android 工作室中的状态 - API 级别 29 - GPS status in android studio - API level 29 Android Studio-Webview API级别-29问题 - Android Studio - Webview Api level - 29 problem 如何在 API 29 中不推荐使用 getExternalStorageDirectory 时读取或写入文件? - How to read or write file as getExternalStorageDirectory is deprecated in API 29? 如何在api级别29中使用android中的根目录建立目录? - how to make directories using the root directory in android in api level 29? boot_completed 不工作 Android 10 Q API 级别 29 - boot_completed not working on Android 10 Q API level 29
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM