簡體   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 . 但我不知道如何使用 CallScreeningService 來獲取來電電話號碼。 任何人都可以幫助我嗎?

正如@Saurabh 所說,篩選呼叫的新方法是通過CallScreeningService 但是,要讓服務在 Android Q 及更高版本上運行,用戶需要將您的應用程序設置為默認呼叫者 ID 和垃圾郵件應用程序(通過使用新的RoleManager類完成)

  1. 注冊您的篩查服務:

     <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. 為您創建服務 class:

     @RequiresApi(api = Build.VERSION_CODES.N) class ScreeningService: CallScreeningService() { override fun onScreenCall(details: Details) { //code here } }
  3. 在您的主要活動(或您認為合適的任何地方)中向用戶請求篩選角色:

     @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. 捕捉用戶的響應:

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

抱歉使用硬編碼的請求代碼>.<

像這樣創建一個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>

我知道現在已經很晚了,但是如果有人有類似的問題,我找到了一個適用於 TelephonyManager 的 API 28+ 的解決方案。 僅僅識別電話號碼 CallScreeningService 是一種過度殺傷,您可能會覆蓋用戶的私人設置或阻止垃圾郵件呼叫的其他應用程序。

您需要在清單中添加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);
            }
        }

如果用戶允許您的應用程序讀取通話記錄,則在廣播接收器點擊 OnReceive => ActionPhoneStateChanged 電話號碼后,電話號碼將在第一次為空,但第二次應填充。 所以准備好在 Api 28+ 電話號碼可以在第二次識別。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM