Making phone calls from your WP7 App

by CameronM 1. September 2011 05:54

Now while there are tons of great things you can do with a Smart Phone, let’s not forget that at the end of the day, it is a phone. Thankfully, Windows Phone gives developers a couple of easy methods to select contacts and make phone calls. This is extremely useful in app where you provide a list of businesses and you want to provide a way for users to call them directly from you app.

As with other Launcher tasks we’ve looked at, such as EmailComposeTask and WebBrowserTask, the code is very simple. Give the PhoneCallTask a phone number and a display name, and the native phone call app takes over.

private void Button_Click(object sender, RoutedEventArgs e)
{
    PhoneCallTask call = new PhoneCallTask();
    call.DisplayName = "Pizza Works";
    call.PhoneNumber = "555 1234";
    call.Show();
}

As with many tasks, the user confirms that they want to carry out the selected task. This give them the peace of mind of knowing that they can click around in your app and wont suddenly start a phone call. 

Another related feature is the PhoneNumberChooserTask. Unlike launcher tasks, chooser tasks use a native OS app to return values back to your app. We’ve seen them at work in the CameraCaptureTask and PhotoChooserTasks that return an image back to your application. The PhoneNumberChooserTask as you would expect, enables the user to select a contact from phone’s contact application and returns their name and phone number, so that your app can then use the PhoneCallTask to initiate a phone call.

Then main difference in the code for a chooser task is that we have to wire up the Completed event, which is triggered when the user either selects a contact, or cancel out of the contact application.

private void Button_Click(object sender, RoutedEventArgs e)

{

    PhoneNumberChooserTask phone = new PhoneNumberChooserTask();

    phone.Completed += new EventHandler<PhoneNumberResult>(phone_Completed);

    phone.Show();

}

 

void phone_Completed(object sender, PhoneNumberResult e)

{

    //only continue if the user selected a contact

    if (e.TaskResult == TaskResult.OK)

    {

        PhoneCallTask call = new PhoneCallTask();

        //e.DisplayName is not returned prior to Mango

        call.DisplayName = e.DisplayName;

        call.PhoneNumber = e.PhoneNumber;

        call.Show();

    }

}

 

 

Tags: ,

WP7