获取 Android 10 上的来电号码编程方法

huangapple go评论64阅读模式
英文:

Get incoming call number programmatically on android 10

问题

查看以前的解决方案,针对此问题的现在已经过时,自 API 29(Android 10)起,已经不能使用(https://developer.android.com/reference/android/telephony/TelephonyManager#EXTRA_INCOMING_NUMBER)。是否有人成功获取了 API 29 上的来电号码?显然,现在要做到这一点,您需要使用 CallScreeningService(https://developer.android.com/reference/android/telecom/CallScreeningService)。

英文:

Looking at previous solutions for this problem are now depreciated as of 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

答案1

得分: 0

是的,在清单文件中实现该类并添加以下所需权限:

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

onScreenCall(Call.Details details) 方法中,您可以调用 details.getHandle() 来获取呼入电话的电话号码。只有在号码无法与设备上现有的联系人信息匹配时,才会调用此方法。

@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(); // 这是呼叫号码
            respondToCall(details, response.build());
        }
    }
}
英文:

Yes, implement the class and add the necessary permission below in the manifest:

  &lt;service
        android:name=&quot;.CallScreeningService&quot;
        android:permission=&quot;android.permission.BIND_SCREENING_SERVICE&quot;&gt;
        &lt;intent-filter&gt;
            &lt;action android:name=&quot;android.telecom.CallScreeningService&quot; /&gt;
        &lt;/intent-filter&gt;
    &lt;/service&gt;

Within onScreenCall(Call.Details details) you can call details.getHandle() which returns the telephone number of the incoming call. 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 &gt;= 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());

        }
    }
}

huangapple
  • 本文由 发表于 2020年8月20日 03:43:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63493926.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定