我正在使用以下代码在清单中注册服务。
<service android:name=".CallScreenService"
android:permission="android.permission.BIND_SCREENING_SERVICE">
<intent-filter>
<action android:name="android.telecom.CallScreeningService"/>
</intent-filter>
</service>
这是我的服务类,它扩展了 CallScreeningService。
@RequiresApi(api = Build.VERSION_CODES.N)
public class CallScreenService extends CallScreeningService {
@Override
public void onScreenCall(Call.Details callDetails) {
CallResponse.Builder response = new CallResponse.Builder();
Toast.makeText(this, callDetails.getHandle().toString(), Toast.LENGTH_LONG).show();
}
}
您必须先注册您的应用程序以进行呼叫筛选角色:
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);
}
@Override
public 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
}
}
}
如果您已实施
onStartCommand
,请将其删除。也删除 onBind
和 onUnbind
。
需要权限
permission.READ_CALL_LOG,permission.READ_CONTACTS