Android 10 API 29 上的 CallScreeningService getExtras NULL

CallScreeningService getExtras NULL on Android 10 API 29

我正在使用 Android Class CallScreeningService onScreenCall(Call.Details calldetails) 获取所有来电,一切正常!从现在开始,我有一个错误,在 Android 10 台设备上,函数 calldetails.getExras() 和 calldetails.getIntentExtras() 总是 returns NULL,而不是 Bundle,我可以在其中阅读一些进一步的信息。在 Android 9 台及更早的设备上一切正常。

有人有类似的问题吗?这是源代码和一些声明:

public class IncomingCallService extends CallScreeningService {

    @Override
    public void onScreenCall(Call.Details callDetails) {
                if (callDetails.getExtras() != null) {
                        Log.d(LOG_TAG, "Everything works on Android 9 or older");
                }else{
                        Log.d(LOG_TAG, "Its Null on Android 10!");
                }  

                if (callDetails.getIntentExtras() != null) {
                        Log.d(LOG_TAG, "Everything works on Android 9 or older");
                }else{
                        Log.d(LOG_TAG, "Its Null on Android 10!");
                }     
}

和Manifest.xml:

<service android:name=".call_handler.IncomingCallService"
    android:permission="android.permission.BIND_SCREENING_SERVICE">
     <intent-filter>
          <action android:name="android.telecom.CallScreeningService"/>
     </intent-filter>
</service>

根据 Android 文档:

Note: The Call.Details instance provided to a call screening service will only have the following properties set. The rest of the Call.Details properties will be set to their default value or null.

Call.Details#getCallDirection()

Call.Details#getConnectTimeMillis()

Call.Details#getCreationTimeMillis()

Call.Details#getHandle()

Call.Details#getHandlePresentation()

Only calls where the Call.Details#getHandle() Uri#getScheme() is PhoneAccount#SCHEME_TEL are passed for call screening. Further, only calls which are not in the user's contacts are passed for screening. For outgoing calls, no post-dial digits are passed.

所以您所看到的只是 Android 10.

的预期行为

在 Android 29+ 上,您需要用户许可:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        RoleManager roleManager = (RoleManager) getSystemService(ROLE_SERVICE);
        Intent intent = roleManager.createRequestRoleIntent(RoleManager.ROLE_CALL_SCREENING);
        startActivityForResult(intent, 1);
    }