Wednesday, November 9, 2011

Launchers and Choosers in Mango Unleashed - Part III - Email, SMS, Contacts and Call

Hello everyone! In this blog post, we will explore the different APIs provided in Mango related to Email, SMS and Phone Call (3 primary ways you want to communicate with folks in addition to the other social networking sites).

Mango provides you different APIs related to Email, SMS and Phone Call in form of "Lanchers and Choosers" :

1. Task(s) related to SMS - As of today, there is a single launcher available named "SmsComposeTask" which is used to send an SMS to a contact. The properties and methods of SmsComposeTask are -
1. Body - Body of the text message to be sent to the recipient
2. To - Recipient Name as present in the phone
3. Show() - This method will launch the Messaging task.

For example  -
SmsComposeTask smsComposeTask = new SmsComposeTask();
smsComposeTask.To = "Karan Vohra";smsComposeTask.Body ="Hi..Wassup!";
smsComposeTask.Show();

With the above values, the "To" and "Body" attributes of SMS will be pre-populated as follows -


Note - On emulator, if you try sending the SMS, the message will not be sent to the contact. Also, when the SMS Compose Task is launched, the current running application is de-activated, so make sure that you save the data in either transient and permanent storage.

2. Task(s) related to Phone Call -

There are three major Launcher and Choosers available related to Phone Calls -
a. PhoneCallTask - This launcher can be used to call a phone number.
b. SavePhoneNumberTask - This chooser can be used to save a phone number along with a display name.
c. PhoneNumberChooserTask - This can be used along with Phone Call Task to choose and call a contact.

Let us use Phone Call Task and Phone Number Choose Task with following application -

We will create a simple application which will allow the end user to choose a phone number from list of stored contacts and upon selecting the contact, call will be made to him/her.

On Page_Load of this application, Phone Number Choose Task is called in the following manner -

PhoneNumberChooserTasknumberChooseTask = new PhoneNumberChooserTask();

numberChooseTask.Completed += new EventHandler<PhoneNumberResult>(numberChooseTask_Completed);

numberChooseTask.Show();





Upon selecting a contact, we need to make call to that contact. This can be accomplished using the following piece of code -

void numberChooseTask_Completed(object sender, PhoneNumberResult e) {
string phoneNumber = String.Empty;
string displayName = String.Empty;
PhoneCallTask phoneCallTask = null;
if (e.Error != null) {
Exception exception = e.Error;

Dispatcher.BeginInvoke(() => { MessageBox.Show(exception.Message); });
}else {
if (e.TaskResult == TaskResult.OK) {
phoneNumber = e.PhoneNumber;
displayName = e.DisplayName;
phoneCallTask =
new PhoneCallTask();
phoneCallTask.DisplayName = displayName;
phoneCallTask.PhoneNumber = phoneNumber;
phoneCallTask.Show();
}

}

}


Before completing this topic, let us also cover how SavePhoneNumberTask works. It is as simple as the following 4 lines of code -

SavePhoneNumberTask savePhoneNumberTask = new SavePhoneNumberTask();

savePhoneNumberTask.PhoneNumber = "999999999";

savePhoneNumberTask.Completed += new EventHandler<TaskEventArgs>(savePhoneNumberTask_Completed);

savePhoneNumberTask.Show();

On running this peice of code on emulator and on phone, following happens in sequence -








3. Task(s) related to Email - There are two tasks related to Email - One to choose the email addresses from contacts stored in phone and the other to save the email address to phone -
SaveEmailAddressTask and EmailAddressChooserTask.

The only properties for SaveEmailAddressTask are  - Email Address (string) and Display Name (string).

The properties that can be retrieved from EmailAddressChooserTask in Completed event are EmailAddress and DisplayName.

The usage of these are same of as of the phone tasks and SMS tasks!!

No comments:

Post a Comment