Contacts and Messaging

0
164

Mobile computing is typically an activity based around contacts and messaging. All Windows Phone 7 devices support a contacts list, standard phone capability, SMS (text) messaging, and email. An application can integrate with these features
to make it easier for users to add specific contacts to their contacts list, make a phone call, send pre-populated SMS messages, and send pre-populated emails to their contacts. All of these features display screens where the user must manually confirm the action. For security reasons, your code cannot perform any of these tasks in the background without the user being aware.

The contacts and messaging functionality is managed by the operating system, but your applications can invoke the appropriate task launcher or chooser to initiate it. For example, you can prompt the user to call to a specific phone number
using the PhoneCallTask launcher. The user can click the call button to initiate the call or click the don’t call button to cancel the operation and close the launcher.

The following task launchers and choosers are available for contacts and messaging:

  • SaveEmailAddressTask. This makes it easy for a user to add an email address you specify in your code to their contacts list. You can use the task callback function to check if the user actually did add the email address to their contacts list.
  • SavePhoneNumberTask. This makes it easy for a user to add a phone number you specify in your code to their contacts list. You can use the task callback function to check if the user actually did add the phone number to
    their contacts list.
  • EmailAddressChooserTask. This allows the user to select a specific email address from their contacts list. The selected address is available to code in the task callback function.
  • PhoneNumberChooserTask. This allows the user to select a specific phone number from their contacts list. The selected number is available to code in the task callback function.
  • PhoneCallTask. This prompts the user to dial a number you specify in your code. It does not provide a callback.
  • EmailComposeTask. This opens the email editor with the To and CC addresses and the subject and message body text set to values that you specify in your code. It does not provide a callback.
  • SmsComposeTask. This opens the SMS message editor with the phone number and message text set to values that you specify in your code. It does not provide a callback.

To use these task launchers and choosers, you must do the following:

  • Create a new instance of the appropriate task launcher or chooser class.
  • Specify the properties of the task launcher or chooser. For example, set the number to call and, optionally, a display name for the PhoneCallTask.
  • If the task launcher or chooser provides a callback, specify a function or lambda expression that will be executed when the user completes the task launcher or chooser.
  • Show the task launcher or chooser.

Some of the task launchers and choosers accept a reference to a callback that is executed when it completes. For example, the EmailAddressChooserTask passes the chosen email address to the callback inside an instance of the EmailResult arguments class. All the callbacks receive a task-specific arguments type, and all of these include
a TaskResult property that indicates if the task was completed successfully or cancelled by the user.

The following code example shows the simple case where the task does not provide callback functionality. It invokes the phone dialing feature with a specified phone number displayed. The user dials the number by clicking the call button.

C#
PhoneCallTask phoneTask = new PhoneCallTask();
phoneTask.DisplayName = “Tailspin Support”;
phoneTask.PhoneNumber = “5551234567”;
phoneTask.Show();

For some tasks that do accept a callback, such as the Save EmailAddressTask and SavePhoneNumberTask, you may not be interested in whether the user actually did add the suggested contact to their contacts list. In this case, you simply omit setting the callback for the task. The following code example shows how you can prompt a user to add a specific email address (with a display name) to the contacts list.

C#
SaveEmailAddressTask saveEmaiTask = new SaveEmailAddressTask();
saveEmaiTask.Email = “Tailspin <[email protected]>”;
saveEmaiTask.Show();

For tasks where you do specify a callback, you access the result though the event argument passed to the callback handler. For example, the following code invokes the chooser for an email address and specifies a callback named EmailChooser_Completed.

C#
EmailAddressChooserTask emailAddressTask =
new EmailAddressChooserTask();
emailAddressTask.Completed
+= new EventHandler<EmailResult>(EmailChooser_Completed);
emailAddressTask.Show();

The callback can check if the user selected an email address. If so, it invokes the email editor with a pre-populated email ready for the user to edit and send.

C#
void EmailChooser_Completed(object sender, EmailResult result)
{
if (result.TaskResult == TaskResult.OK)
{
EmailComposeTask emailComposeTask = new EmailComposeTask();
emailComposeTask.To = result.Email;
emailComposeTask.Subject = “My Message Subject”;
emailComposeTask.Body = “This is the content of my message”;
emailComposeTask.Show();
}
}

For more information about the tasks described in this section, and the other types of tasks available on Windows Phone 7 devices, see “Microsoft.Phone.Tasks Namespace” on MSDN (http://msdn.microsoft.com/en-us/library/microsoft.phone.tasks(VS.92).aspx).