Integrating Applications with the Windows Phone OS : Working with Launchers and Choosers

2/15/2011 11:47:41 AM
In this section, you will explore how to work with launchers and choosers from within your application. You will use PhoneNumberChooserTask and SmsComposeTask to create an application that selects a contact from the shared list of contacts on the phone and then composes a text message to that contact's phone.

1. Creating the User Interface

The user interface for this sample consists of a single button; when the user clicks this button, a list of contacts will be displayed, allowing the user to pick one.

  1. Launch Visual Studio 2010 Express, and select the Windows Phone Application template. Change the project name to "Tasks," select OK, and Visual Studio will set up a new project.

  2. Open MainPage.xaml in design mode, and add a button to the page. Change the button's caption to be "Send SMS."

2. Coding Application Logic

When the user clicks the "Send SMS" button of your application, she should be presented with a list of contacts available on your device. Conveniently for us, even on the emulator, Microsoft has included a sample list of contacts for us to test an application with.

  1. Open MainPage.xaml.cs (right-click MainPage.xaml and select View Code). At the top of the page, add the following using statement:

    using Microsoft.Phone.Tasks;

  2. Declare the following module-level variable for the PhoneNumberChooserTask chooser (insert it right above the MainPage() constructor):

    private PhoneNumberChooserTask _choosePhoneNumberTask;

  3. Instantiate a new PhoneNumberChooserTask object within the MainPage() constructor, and associate a method to invoke when the user selects a contact from the list. The method to call upon the chooser's return will accept the phone number selected as one of the parameters. Use the following two lines of code to accomplish that:

    _choosePhoneNumberTask = new PhoneNumberChooserTask();
    _choosePhoneNumberTask.Completed += new

  4. Code the ChoosePhoneNumberTaskCompleted method that will be invoked upon selection of a contact from the list. Note the use of an SmsComposeTask launcher to create a new text message to the person you have selected from the contact list:

    private void ChoosePhoneNumberTaskCompleted(object sender, PhoneNumberResult e)
    new SmsComposeTask() { Body = "SMS using Windows Phone 7 Chooser", To =
    e.PhoneNumber }.Show();

  5. Finally, add code to react to the button-click event by opening the PhoneNumberChooser launcher. The easiest way to accomplish this is to double-click the button with MainPage.xaml open in design view and make the button-click event look like the following:

    private void button1_Click(object sender, RoutedEventArgs e)

  6. Press F5 to run your application. Click the Send SMS button, and select Andrew R. (Andy) Hill from the list of contacts that comes up on the emulator. Immediately after selecting Andy, you should see a screen similar to Figure 1, where the SMS message has been composed and is ready to be sent to Andrew.

To summarize what you have learned so far, your application integrates with the Windows Phone OS via a set of API methods referred to as launchers and choosers. Working with launchers and choosers is fairly straightforward, as illustrated by the foregoing example.

One major limitation of mobile platforms is their inherently short battery life. This limitation causes OS designers and phone manufacturers to come up with various techniques to balance short battery life with positive user experience. One such technique is to allow only one application to run on the phone at any given time. You may be wondering then, what happens when your application yields execution to the built-in Windows Phone OS application invoked with the launcher? That brings us to the important Windows Phone concept of application tombstoning, a subject you'll explore along with the Windows Phoneapplication life cycle in the next section.

Figure 1. SMS is composed and ready to go to Andrew.

  •  Introducing Windows Phone 7 Launchers and Choosers
  •  Java Mobile Edition Security : Development and Security Testing (part 3) - Code Security & Application Packaging and Distribution
  •  Java Mobile Edition Security : Development and Security Testing (part 2) - Reverse Engineering and Debugging
  •  Java Mobile Edition Security : Development and Security Testing (part 1) - Configuring a Development Environment and Installing New Platforms & Emulator
  •  Java Mobile Edition Security : Configurations, Profiles, and JSRs
  •  Programming the Mobile Web : Performance Optimization
  •  Programming the Mobile Web : Testing and Debugging (part 3) - Client-Side Debugging
  •  Programming the Mobile Web : Testing and Debugging (part 2) - Server-Side Debugging & Markup Debugging
  •  Programming the Mobile Web : Testing and Debugging (part 1) - Remote Labs
  •  Windows Phone 7 : Working with Controls and Themes - Adding Transition Effects
  •  Windows Phone 7 : Working with Controls and Themes - Understanding Frame and Page Navigation
  •  Windows Phone 7 : Working with Controls and Themes - Panorama and Pivot Controls
  •  Programming the Mobile Web : Widgets and Offline Webapps - Platforms (part 5) - Windows Mobile & BlackBerry
  •  Programming the Mobile Web : Widgets and Offline Webapps - Platforms (part 4) - Windows Mobile & BlackBerry
  •  Programming the Mobile Web : Widgets and Offline Webapps - Platforms (part 3) - webOS & Android
  •  Programming the Mobile Web : Widgets and Offline Webapps - Platforms (part 2) - iPhone, iPod, and iPad
  •  Programming the Mobile Web : Widgets and Offline Webapps - Platforms (part 1) - Symbian/Nokia
  •  Programming the Mobile Web : Widgets and Offline Webapps - Standards
  •  Mobile Application Security : BlackBerry Security - Networking
  •  Mobile Application Security : BlackBerry Security - Local Data Storage
    Top 10
    Windows 7 : Searching Your Computer (part 1)
    Programming Microsoft SQL Server 2005 : FOR XML Commands (part 1) - FOR XML RAW & FOR XML AUTO
    Software Overclocking for Windows (Part 1)
    Programming Excel with VBA and .NET : Knowing the Basics - Classes and Modules
    Using SQL Server 2005 Integration Services : Extensibility (part 3) - Script Components
    Integrating Office Communications Server 2007 in an Exchange Server 2010 Environment : Web Conferencing
    iPhone 3D Programming : Adding Textures to ModelViewer (part 1) - Enhancing IResourceManager
    Cheap smartphones at Computex 2012 : Acer CloudMobile S500, Gigabyte GSmart G1362, Malata Z500
    Samsung EX2F : Samsung's Compact Camera With F/1.4 Extensive Lens
    Working with Assemblies : Overview of Security Changes in .NET 4.0
    Most View
    Wedge Mobile Keyboard And Touch Mouse for Windows 8
    Wireless Networking in Vista
    Apple’s Sandboxing Security Issue
    SQL Server 2008 : Advanced Stored Procedure Programming and Optimization - Using Cursors in Stored Procedures
    Plextor M3 Pro 256GB
    Advanced ASP.NET : The Entity Framework (part 1) - Creating an Entity Data Model
    Consumers Finally Dropping XP In Favour Of Windows 7
    Transact-SQL in SQL Server 2008 : Change Tracking (part 1) - Implementing Change Tracking
    Windows Server 2008 and Windows Vista : Architectural Parts of a GPO (part 2) - Group Policy Container
    Samsung Series 7 CHRONOS - Time, Space And Power
    iPhone Application Development : Getting the User’s Attention - Exploring User Alert Methods
    Macro Marvel by Peiling Lee
    Upgrading to Windows Server 2003 : Updating the Active Directory Schema - Verifying the Forest Schema Update
    LittleFox 1.8.78 - Gives Firefox Users A Happy Middle
    Low- Pass Filter Removal (Part 3)
    Ten Keys to Successful Microsoft Business Intelligence (cont.)
    CPU System Workshop (Part 5) - ZALMAN CNPS11X PERFORMA, Corsair Vengenance K60
    Managing User Account Control and Elevation Prompts
    Philips Blade 224CL2 - The Sharpest Blade
    Using MySQL Enterprise (part 3) - Query Analyzer