androidandroid-serviceandroid-manifestandroid-permissionsandroid-service-binding

How to bind CallScreeningService?


I want to get the Call Details and block the calls(if necessary). As the TelecomManager endCall method is deprecated and as per the documentation it is suggesting to use the CallScreeningService. https://developer.android.com/reference/android/telecom/CallScreeningService.html

As mentioned in the Android documentation, I am trying to bind the CallScreeningService with my application.

I have created a class

    public class CallUtil extends CallScreeningService {
      private Call.Details mDetails;
      private static CallScreeningUtil sCallScreeningUtil;

      @Override
      public void onScreenCall(Call.Details callDetails) {
        CallResponse.Builder response = new CallResponse.Builder();
        Log.e("CallBouncer", "Call screening service triggered");

        sCallScreeningUtil = this;
        mDetails = callDetails;
        respondToCall(callDetails, response.build() );
    }
}

This is a system app and I have added necessary permission in AndroidManifest.xml such as CALL_PHONE, MODIFY_PHONE_STATE, CALL_PHONE, ANSWER_PHONE_CALLS.

I have added the Service details as well like below,

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

I am kinda lost on how to bind this service with my activity or how do I bind this service with my application that will call the Overridden methods in CallUtil.


Solution

  • Based on the documentation provided over here https://android.googlesource.com/platform/frameworks/base/+/9e1d4f86ba43e87264aba178f2bb037a3c3b26fb/telecomm/java/android/telecom/CallScreeningService.java

        Intent mCallServiceIntent = new Intent(this,"android.telecom.CallScreeningService");
            ServiceConnection mServiceConnection = new ServiceConnection(){
    
                @Override
                public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                    // iBinder is an instance of CallScreeningService.CallScreenBinder
                    // CallScreenBinder is an inner class present inside CallScreenService
                }
    
                @Override
                public void onServiceDisconnected(ComponentName componentName) {
    
                }
    
                @Override
                public void onBindingDied(ComponentName name) {
    
                }
            }
    

    And from an activity, to bind to the service you can use

    bindService(mCallServiceIntent, mServiceConnection, Context.BIND_AUTO_CREATE)