Click here to Skip to main content
15,882,017 members
Articles / Mobile Apps / Windows Phone 7

How to Call a Number in WP7 using the PhoneCallTask?

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
12 Apr 2012CPOL2 min read 12.7K   1
How to call a number programmatically in Windows Phone

Previously, we learnt “How to Save a Phone Number in WP7 using the SavePhoneNumberTask?”. There, we discussed the basics of the API and details about the implementation stuff with step-by-step details.

Today, in this small blog post, we will learn how to call a number programmatically in Windows Phone 7 by using the PhoneCallTask API. We will also know what the limitations are of this API class. Continue reading to know further.

Know About the API

Like other phone tasks, PhoneCallTask is also a sealed class present inside the Microsoft.Phone.Tasks namespace. It exposes two properties called DisplayName and PhoneNumber of type string. The Show() method shows the phone application where it confirms the user whether to dial or not.

Here is the meta data of PhoneCallTask:

C#
namespace Microsoft.Phone.Tasks
{
    public sealed class PhoneCallTask
    {
        public string DisplayName { get; set; }
        public string PhoneNumber { get; set; }

        [SecuritySafeCritical]
        public void Show();
    }
}

Here is the internal implementation of the Show() method call:

C#
[SecuritySafeCritical]
public void Show()
{
    if (!ChooserHelper.NavigationInProgressGuard((Action) (() => this.Show()))) return;
    ThreadPool.QueueUserWorkItem(new WaitCallback(PhoneCallTask.PhoneDial), (object) this);
}

[SecuritySafeCritical]
private static void PhoneDial(object phoneCallTask)
{
    PhoneCallTask phoneCallTask1 = phoneCallTask as PhoneCallTask;
    string phoneNumber = phoneCallTask1.PhoneNumber;
    string displayName = phoneCallTask1.DisplayName;
    string.IsNullOrEmpty(phoneNumber);
}

I hope that the above internal code will help you to understand the actual implementation of the API which exposes by the SDK for the developers.

Implementation Steps

The implementation steps of PhoneCallTask in your application is really very simple. Just create the instance of the class by populating the properties and call the Show() method as shown in the below code snippet:

C#
var phoneCallTask = new PhoneCallTask
                        {
                            DisplayName = "Kunal Chowdhury",
                            PhoneNumber = "0208795446322"
                        };
phoneCallTask.Show();

The property called “PhoneNumber” is required to dial the number using the PhoneCallTask. You can skip “DisplayName” but “PhoneNumber” is mandatory.

Screenshot 1: How to Call a Number in WP7 using the PhoneCallTask?     Screenshot 2: How to Call a Number in WP7 using the PhoneCallTask?

Limitations

There are some limitations of this class for security purposes. Using PhoneCallTask, you can dial any number except any special numbers or service codes, e.g., Balance Check like *111*1#.

To check it out, set the PhoneNumber property to any special numbers like: *111*1# and call the Show() method as shown in the below code snippet:

C#
var serviceCallTask = new PhoneCallTask
                          {
                              DisplayName = "Balance Check", 
                              PhoneNumber = "*111*1#" // service call number
                          };
serviceCallTask.Show();

When you run the code, you will notice the below screen if you dial the number from the code:

Screenshot 3: How to Call a Number in WP7 using the PhoneCallTask?

This limitation is made to protect the user from any security hole from any application installed from 3rd party.

I hope that this post was helpful to you to understand the basics and implementation of the API. If you like these posts, don’t forget to share with your friends, colleagues or others for whom you believe this post will be useful. Don’t forget to share your feedback as it will help me to improve my future posts and also encourage me to deliver more for the benefit of you.

Stay tuned to my blog, Twitter, or Facebook to read more articles, tutorials, news, tips & tricks on various technology fields.

Reference: http://www.kunal-chowdhury.com.

You may like to follow me on Twitter @kunal2383 or may like the Facebook page of my blog http://www.facebook.com/blog.kunal2383.

This article was originally posted at http://www.kunal-chowdhury.com/feeds/posts/default

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India

Kunal Chowdhury is a former Microsoft "Windows Platform Development" MVP (Most Valuable Professional, 2010 - 2018), a Codeproject Mentor, Speaker in various Microsoft events, Author, passionate Blogger and a Senior Technical Lead by profession.

He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows app development. He posts his findings, articles, tutorials in his technical blog (www.kunal-chowdhury.com) and CodeProject.


Books authored:


Connect with Kunal on:





Comments and Discussions

 
QuestionFormatting of the code blocks Pin
Wendelius11-Apr-12 18:12
mentorWendelius11-Apr-12 18:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.