How to use CallScreeningService in android to reject the incoming call

2k Views Asked by At

I am trying to add CallScreeningService in my android application(targetSdkVersion 29) without making my app a caller app. I follow all the instructions but still not getting it properly OR missing something there. Here is my code:

public class CallAppService extends CallScreeningService {
    @Override
    public void onScreenCall(Call.Details callDetails) {
        Log.e("CallBouncer", "Call screening service triggered");
        respondToCall(callDetails, new CallScreeningService.CallResponse.Builder().setDisallowCall(true).setRejectCall(true).build());
    }
}

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

Added following permission:

 <uses-permission android:name="android.Manifest.permission.READ_CONTACTS"/>
    <uses-permission android:name="android.Manifest.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.Manifest.permission.READ_CALL_LOG"/>
    <uses-permission android:name="android.Manifest.permission.PROCESS_OUTGOING_CALLS"/>
    <uses-permission android:name="android.Manifest.permission.ANSWER_PHONE_CALLS"/>

But still that this service is not invoking when I do a call. Is there anything that I am missing? I also see this issue but not getting the idea that why we need a ServiceConnection as it is not mentioned in the documentation.

2

There are 2 best solutions below

0
On
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        RoleManager roleManager = (RoleManager) getSystemService(ROLE_SERVICE);
        Intent intent = roleManager.createRequestRoleIntent(RoleManager.ROLE_CALL_SCREENING);
        startActivityForResult(intent, 1);
    }
0
On

You can refer to the answer to another question (get incoming number by CallScreeningService):

I want to get hold of phone number Programmatically as the phone is ringing in android

And the only difference is that you should use your own callResponse instead to reject it.