1、Manifest中声明
<service android:name="your.package.YourCallScreeningServiceImplementation"android:permission="android.permission.BIND_SCREENING_SERVICE"><intent-filter><action android:name="android.telecom.CallScreeningService"/></intent-filter></service>
2、创建YourCallScreeningServiceImplementation
public class YourCallScreeningServiceImplementation extends CallScreeningService {@Overridepublic void onScreenCall(@NonNull Call.Details callDetails) {respondToCall(callDetails,new CallResponse.Builder().setDisallowCall(false).build());}}
3、设置为默认应用
private static final int REQUEST_ID = 1;public void requestRole() {RoleManager roleManager = (RoleManager) getSystemService(ROLE_SERVICE);Intent intent = roleManager.createRequestRoleIntent(RoleManager.ROLE_CALL_SCREENING);startActivityForResult(intent, REQUEST_ID);}@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent data) {if (requestCode == REQUEST_ID) {if (resultCode == android.app.Activity.RESULT_OK) {// Your app is now the call screening app} else {// Your app is not the call screening app}}}
遇到的问题
1、在小米14上需要联系人权限,否则onScreenCall()不执行
2、鸿蒙系统上如果设置默认应用失败,检测一下manifest中的权限声明,目前发现需要删除
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
这个权限是Android13开始有的,对鸿蒙系统没有影响,可以在代码编译阶段删除android.permission.POST_NOTIFICATIONS