telephonymanager.extra\u传入的\u编号在api级别29中不推荐使用

aoyhnmkz  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(438)

我现在需要确认 android中的来电号码,但使用时 telephonymanager.extra\u传入\u号码android studio警告 额外的\传入\号码已被否决。我浏览了developers.android.com,它显示执行呼叫屏蔽的应用程序应该改用callscreeningservice api。但我不知道如何使用callscreeningservice获取来电号码。有人能帮我吗?

hk8txs48

hk8txs481#

创建 CallScreeningService 这样地:

class ScreeningService : CallScreeningService() {

    override fun onScreenCall(callDetails: Call.Details) {
        val phoneNumber = callDetails.handle.schemeSpecificPart
        // Do stuff with phone number
    }
}

并在您的帐户中注册此服务 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>
wkyowqbh

wkyowqbh2#

正如@saurabh所说,屏蔽通话的新方法是通过 CallScreeningService . 但是,要让服务在android q及以上版本上运行,用户需要将您的应用程序设置为默认的来电显示和垃圾邮件应用程序(这是通过使用新的 RoleManager 类)
注册筛选服务:

<service android:name="com.example.ScreeningService"
     android:permission="android.permission.BIND_SCREENING_SERVICE">
     <intent-filter>
         <action android:name="android.telecom.CallScreeningService"/>
     </intent-filter>
 </service>

创建服务类:

@RequiresApi(api = Build.VERSION_CODES.N)
 class ScreeningService : CallScreeningService() {

     override fun onScreenCall(details: Details) {
         //code here
     }

 }

在您的主要活动中向用户请求筛选角色(或在您认为合适的情况下):

@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!
     }
 }

捕捉用户的响应:

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 -> {}
         }
     }

抱歉使用硬编码请求代码><

相关问题