MOBILE

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
    EventHandler<PhoneNumberResult>(ChoosePhoneNumberTaskCompleted);

  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)
    {
    _choosePhoneNumberTask.Show();
    }

  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.

Other  
  •  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
  •  
    Most View
    AMD Radeon HD 7850 2GB vs. Nvidia GeForce GTX 660 2GB vs. AMD Radeon HD 7870 2GB (Part 1)
    Binding Application Data to the UI objects in Silverlight
    Tasmania - Ideal Destination For Landscape Photographers (Part 2)
    Exchange Server 2010 : Deploying a Database Availability Group (part 3)
    Extra Network Hardware Round-Up (Part 3)
    Building Out Of Browser Silverlight Applications - Controlling the Application Window
    Philips Sound Tower DCM5090 - Elegant and Versatile
    Samsung 45mm f/1.8 NX i-Function Lens
    Design for Mobile : Sketches (part 1) - Finding Sightings By Location
    DIY - Make Your Own CMS (Part 1)
    Top 10
    Windows Phone 8 In-Depth Review (Part 6)
    Windows Phone 8 In-Depth Review (Part 5)
    Windows Phone 8 In-Depth Review (Part 4)
    Windows Phone 8 In-Depth Review (Part 3)
    Windows Phone 8 In-Depth Review (Part 2)
    Windows Phone 8 In-Depth Review (Part 1)
    Xiaomi Phone 2 - High-End Specifications In A Surprisingly Cheap Package (Part 5)
    Xiaomi Phone 2 - High-End Specifications In A Surprisingly Cheap Package (Part 4)
    Xiaomi Phone 2 - High-End Specifications In A Surprisingly Cheap Package (Part 3)
    Xiaomi Phone 2 - High-End Specifications In A Surprisingly Cheap Package (Part 2)