How to Use Launchers in Your Windows Phone 7 Applications

Introduction

In an attempt to provide isolation, Windows
Phone
7 platform does not provide applications direct access to some phone
features, such as contacts, sending email or taking pictures. This is done to provide
sandboxing. However,
when applications have a legitimate need to access these features directly,
there are some helper APIs that provide this functionality.
These APIs
invoke distinct built-in applications, which replace the currently running
application, to do the task at hand (sending email, taking picture, etc.).

Launchers defined

Launchers are a set of APIs that launch one of the build-in
applications by which the user can complete a task for which the Windows Phone
7 platform provides a native application. An example would be sending an email
or taking a picture via the built-in camera.

Developers will author their applications such that their
application code calls the Launcher APIs with the required information and the
Launcher API will do the specified task. For example, you can provide a phone
number to a Launcher API and it can initiate a phone call and switch from the
currently executing application to the phone application. When the user is
finished with the call, the context switches back to the originally executing
application.

One thing to note is that Launcher APIs do not have a return
value.

Types of Launchers

There are various Launchers available with Windows Phone 7:

  • Email composition
  • For Marketplace detail
  • For Marketplace hub
  • For Marketplace review
  • For Marketplace search
  • For launching Media Player
  • For Phone call
  • For Search
  • For composing SMS
  • For Web browsing

Let’s look at how we can use these Launchers in an
application

 

Hands-On

Let us start by creating a new Silverlight C#
project for Windows Phone. Let’s call it LauncherDemo.

Creating a new Silverlight C# project for Windows Phone
Figure 1: Creating a new Silverlight C# project for Windows Phone

Add a button with the text “Launch Media Player.”

In the MainPage.xaml.cs, add a reference directive for
Microsoft.Phone.Tasks namespace.

On the button click event, add the following code.

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            MediaPlayerLauncher mediaPlayerLauncher = new MediaPlayerLauncher();
            mediaPlayerLauncher.Media = new Uri("http://media.ch9.ms/ch9/e77b/b21d7957-7a8e-4f0b-8cf5-9e8c011de77b/sltv62_ch9.wmv", UriKind.RelativeOrAbsolute);
            mediaPlayerLauncher.Location = MediaLocationType.Data;
            mediaPlayerLauncher.Controls = MediaPlaybackControls.Pause | MediaPlaybackControls.Stop;
            mediaPlayerLauncher.Show();

        }

You will note that we are passing in a media file from the
internet. You can actually see
the video at MSDN Channel 9
.

Now, when you compile and run this application in the
emulator, you will hear the music from the WMV file being
played.

You can verify that you are playing a real internet file by
playing the video on MSDN Channel 9 and verifying if the introductory music is
the same or not.

If you use the Back button while the music is playing, you
will return back to the LauncherDemo application.

If you deploy this XAP file to a phone device, you can
actually see the video with the audio. With the Windows Phone emulator, you
will only hear audio. (That is the limitation of the current version of the
emulator).

If you are having trouble with following up, you can
download the demo code which accompanies this article.

Summary

In this article, we learned how to use Launchers to access
certain phone functionality.B This allows application developers to worry about
their application instead of trying to re-invent the wheel and figure out how
to write a music player (for example).

About the author

Vipul Patel is a Software Engineer currently working at
Microsoft Corporation. He is currently working in the Office Communications
Group and has worked in the .NET team earlier in the Base Class libraries and
the Debugging and Profiling team. He can be reached at vipul_d_patel@hotmail.com

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read