Windows Phone Development–Choosers and Launchers

While doing windows phone application programming you will come across some requirements such as:

a. “Selecting a contact from contact book”

b. “Open up Camera, take a photo and upload or use the photo”

c.  “Open a link in Web Browser”

How you can achieve these typical tasks?

Answer is pretty simple Windows Phone SDK provides a set of API’s or classes you can use to perform these tasks easily.

You can use Launchers and Choosers in your Windows Phone applications to enable users to perform common tasks. By using Launchers and Choosers, you help provide a consistent user experience throughout the Windows Phone platform.

1. Launchers – are certain task API’s that would invoke a built in Application on Windows Phone.

For Example: ‘opening a link on Phone Browser’. Launchers API available with Windows Phone SDK will simplifies the task of implementing such a feature from the scratch.

Some of the Launchers provided by Windows Phone SDK are:

  • Bing Maps Direction Task
  • Bing Maps View Task
  • Connection Settings Task
  • Compose Email Task
  • Market Place Details View Task
  • Market Place Hub Task
  • Market Place Review Task
  • Market Place Search Task
  • Media Player Task
  • Phone Call task
  • Search Task
  • Share a Link Task
  • Share Status
  • SMS Compose
  • Web Browser Task

[More detailed reference use MSDN on Reference on Launchers for Windows Phone]

2. Choosers – are certain task API’s that would invoke a built in Application on Windows Phone and after completing the task it would return the result through application call back methods.  When the new application appears, the user can choose to complete or cancel the task. When the user closes the new application, the calling application is usually reactivated and supplied with data and status.

 For Example: “Selecting a contact from Address book and sending a mail to it”. or “Launching camera application and taking a photo and stores it back on the application”.

Some of the Choosers provided by Windows Phone SDK are:

  • Address Chooser Task
  • Camera Capture Task
  • Email Address Chooser Task
  • Game Invite Task
  • Phone Number Choose Task
  • Photo Chooser Task
  • Save Contact Task
  • Save Email Address Task
  • Save Phone numbers task
  • Save Ringtone Task

Using Choosers and Launchers

Implementing Launchers and Choosers are pretty simple. Most of the launchers and choosers are available within namespace “Microsoft.Phone.Tasks“.

a. To use a Launcher follow the below general steps:

  1. Create an instance of the task type for the Launcher.
  2. Set any required and optional properties of the task object. These determine the behavior of the task application when it is invoked.
  3. Call the Show method of the task object.

Example:

The below example will open a Phone Web Browser with url https://google.com

  private void TestWebBrowserChooser()
        {
            WebBrowserTask webBrowserTask = new WebBrowserTask();
            webBrowserTask.Uri = new Uri("https://www.google.com");
            webBrowserTask.Show(); 
        }

b. To use a Chooser follow the below general steps:

  1. Create an instance of the task type for the Launcher.
  2. Identify the callback method to run after the user completes the task.
  3. Set any required and optional properties of the task object.
  4. Call the Show method of the task object.
  5. Implement the completed event handler to capture data and status after the user completes the task.

Example: (Below example will launch address book and when user completes selecting an address, the call back will happen and data will be passed on to your application)

 private void TestAddressBookChooser()
        {
            AddressChooserTask addressChooserTask = new AddressChooserTask();
            addressChooserTask.Completed += new EventHandler<AddressResult>(addressChooserTask_Completed);
            addressChooserTask.Show();
        }

        void addressChooserTask_Completed(object sender, AddressResult e)
        {
            Debug.WriteLine(e.Address);
            Debug.WriteLine(e.DisplayName);
            
            //throw new NotImplementedException();
        }

Pretty simple isn’t it.

But there are few important concepts you need to understand when implementing launchers and choosers.

  1. When using Launchers and Choosers, is that the calling application is either put into a dormant state or tombstoned when another application is launched.
  2. When the new task application is launched, your application is deactivated and is no longer running.
  3. Usually, your application is put into a dormant state by the operating system.
  4. After completing the task, the user can return to your application that was preserved in memory, and it resumes in the same state as before the new application was launched.
  5. However, it is also possible that your application may be terminated by the operating system. In this case, if the user returns to your application, it is a new instance, and the state of your application is not automatically restored.

Hope you enjoy this post. Will write more soon, keep reading it.

References: 

http://msdn.microsoft.com/en-us/library/ff769550(v=vs.92).aspx

http://msdn.microsoft.com/en-us/library/ff769543(v=vs.92).aspx 

http://msdn.microsoft.com/en-us/library/ff769543(v=vs.92).aspx