简体   繁体   English

在 android 10 上以编程方式获取来电号码

[英]Get incoming call number programmatically on android 10

Looking at previous solutions for this problem are now depreciated as of api 29(Android 10).从 api 29(Android 10) 开始,查看此问题的先前解决方案现在已贬值 Has anyone been able to get the incoming phone number on for api 29. Apparently now to do this you need to use CallScreeningService有没有人能够获得 api 29 的来电号码。显然,现在要做到这一点,您需要使用CallScreeningService

Yes, implement the class and add the necessary permission below in the manifest:是的,实施 class 并在清单中添加以下必要的权限:

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

Within onScreenCall(Call.Details details) you can call details.getHandle() which returns the telephone number of the incoming call.onScreenCall(Call.Details details)中,您可以调用details.getHandle()返回来电的电话号码。 This will only get called if the number cannot be matched to the contact information existing on the device.只有当号码无法与设备上现有的联系信息匹配时,才会调用此方法。

@Override
public void onScreenCall(Call.Details details) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        if(details.getCallDirection() == Call.Details.DIRECTION_INCOMING) {
            CallResponse.Builder response = new CallResponse.Builder();
            response.setDisallowCall(false);
            response.setRejectCall(false);
            response.setSilenceCall(false);
            response.setSkipCallLog(false);
            response.setSkipNotification(false);
            details.getHandle(); //This is the calling number
            respondToCall(details, response.build());

        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM