英文:
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:
<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. 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());
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论