Making phone calls

Our app may need to initiate a phone call from within the app without the user having to leave the current context.

How to do it...

To initiate a call directly from within our app, we use an Intent instance:

  1. We first need to ensure that the device has telephony features. There are two ways to do this; one way is to use attributes:
    [assembly: UsesFeature(
      Android.Content.PM.PackageManager.FeatureTelephony)]

    Alternatively, we can check for the telephony feature using the HasSystemFeature() method at runtime:

    var hasTelephony = PackageManager.HasSystemFeature(
      Android.Content.PM.PackageManager.FeatureTelephony);
    if (hasTelephony) {
      // do something
    }
  2. If we are running on a device with telephony features, we still need to request permissions to start phone calls:
    [assembly: UsesPermission(Manifest.Permission.CallPhone)]
  3. Finally, we can make the phone call using the ActionCall action intent:
    var number = "0123456789";
    var intent = new Intent(Intent.ActionCall);
    intent.SetData(Uri.Parse("tel:" + number));
    StartActivity(intent);

How it works...

We may want to allow the user to start a call directly from our app, and we can do this very easily. This is useful if we want the app to start calling immediately without the user having to press the call button on the phone's dialer.

As some devices, such as Wi-Fi tablets, do not actually have telephony features, we need to ensure that the device has the desired features first. There are two ways to check for the availability of specific features.

We can ensure that the app cannot be installed on a device without telephony features. For this, we use the [UsesFeature] attribute, which will add an entry into the AndroidManifest.xml file. Alternatively, we can check for the availability of specific features at runtime using the HasSystemFeature() method on the PackageManager type.

Tip

To prevent an app from being installed on a device without a certain feature, the [UsesFeature] attribute is used. If the app can still function without the feature, the HasSystemFeature() method can be used at runtime.

We first need to request permission to start calls directly as this may cost the user. So, we request the CallPhone permission.

To place the call, all we need to do is create an intent with the ActionCall intent action. We then specify the number using the SetData mechanism. When we call StartActivity, the phone will begin calling the number.

If multiple phone apps exist on the device, such as Skype and the default dialer, the chooser will be displayed. If there are multiple apps, but the user has selected a default, then the default will be used automatically.

There's more...

If we want to call numbers from our app, without getting the CallPhone permission but only a confirmation from the user to receive the call, we can open the dialer.

To open the dialer we use the ActionDial intent action instead of ActionCall. This will display the dialer with the telephone number already entered, but this will not start the call.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset