0

I have an issue making phone calls from the foreground service and this is my question about that.

As suggested in the comments of the question - I started testing the ConnectionService. I added BIND_TELECOM_CONNECTION_SERVICE permission to the manifest file.

Declared MyConnectionService class:

[Service(Permission= Manifest.Permission.BindTelecomConnectionService)]
public class MyConnectionService : ConnectionService
{

}

And then I run this code:

var telecomManager = (TelecomManager)GetSystemService(Context.TelecomService);
var phoneAccountHandle = new PhoneAccountHandle(
    new ComponentName(this.PackageName, nameof(MyConnectionService)), "MyApp");
var builder = new PhoneAccount.Builder(phoneAccountHandle, "CustomAccount");
var phoneAccount = builder.Build();
telecomManager.RegisterPhoneAccount(phoneAccount);

The telecomManager.RegisterPhoneAccount(phoneAccount); gives me an exception:

Java.Lang.SecurityException: 'PhoneAccount connection service requires BIND_TELECOM_CONNECTION_SERVICE permission.'

Ok - it seems the permission is not granted. Testing it with ActivityCompat.CheckSelfPermission() - yes, it is not granted.

Making a permission request with ActivityCompat.RequestPermissions() - it is executed and that's it. No permission request on the screen, not way to grant on the application permission page.

My question is - how to grant this kind of permission?

Environmet:

  • Android API Level 28
  • Testing on Google Pixel 2 XL phone running Android 10
Tadas Šukys
  • 4,140
  • 4
  • 27
  • 32
  • 1
    You need to declare the `BIND_TELECOM_CONNECTION_SERVICE` perm via the manifest for your `ConnectionService` subclass – SushiHangover Feb 15 '20 at 16:52
  • @SushiHangover I'm registering it in this way [Service(Permission= Manifest.Permission.BindTelecomConnectionService)] public class MyConnectionService : ConnectionService {} - still same result. – Tadas Šukys Feb 15 '20 at 17:54
  • 1
    You will need to use the `Name` parameter for your `ServiceAttribute`, see this answer (#3) for an example: https://stackoverflow.com/a/49415199/4984832 – SushiHangover Feb 15 '20 at 22:09

1 Answers1

0

Comments by SushiHangover helped a lot and pointed to the right direction. In order to make the solution work and avoid the exception, the ConnectionService should have proper name (fully qualified Java class name) and permission declaration. These values can be added either in the manifest file or by declaring the ServiceAttribute in the code:

 [Service(
      Name = "com.sample.MyApp.MyConnectionService", 
      Permission = Manifest.Permission.BindTelecomConnectionService, 
      Enabled = true)]
 public class MyConnectionService : ConnectionService
 {
 }

Secondly, the name of the service must be exact when creating ComponentName instance:

var telecomManager = (TelecomManager)GetSystemService(Context.TelecomService);
var phoneAccountHandle = new PhoneAccountHandle(
    new ComponentName(this.PackageName, "com.sample.MyApp.MyConnectionService"), 
    "GateOpener");
var builder = new PhoneAccount.Builder(phoneAccountHandle, "CustomAccount");
var phoneAccount = builder.Build();
telecomManager.RegisterPhoneAccount(phoneAccount);
Tadas Šukys
  • 4,140
  • 4
  • 27
  • 32