Launchers & Choosers–Part 2

Windows Phone From Scratch #23SMS

In yesterday’s posting we created an application that used a chooser to allow you to pick a name from the contacts list and to obtain the related phone number.

Today we’ll use that information to launch the SMS service to send a message to the selected user.

Today’s posting is very short, as the work of creating a launcher is nearly identical to that of calling a chooser, except that with a launcher you don’t get information back.

You will remember that yesterday we created a private member variable of type PhoneNumberChooserTask.  Today we’ll add a second private member of type SmsComposeTask

 private PhoneNumberChooserTask _choosePhoneNumber;
 private SmsComposeTask _smsCompose;

This time we’ll initialize the Task in the call-back method for the phone number, thus creating a clear logic to the program:

  • Ask for the phone number
  • When you return with the phone number feed it to the SMS Task
  • Launch the SMS Task.

The SmsComposeTask has two properties: the body of the message and the phone number. We’ll add these one by one (though we could just use member initialization) and then, as we did with the Chooser, we’ll call Show to start up the Launcher.  The difference is that there is no call back, as Launchers do not return information.

Here is the complete code behind  – there are no changes to the Xaml:

using System;
using System.Windows;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;

namespace SMS
{
   public partial class MainPage : PhoneApplicationPage
   {
      private PhoneNumberChooserTask
         _choosePhoneNumber;
      private SmsComposeTask _smsCompose;

      public MainPage()
      {
         InitializeComponent();
         _choosePhoneNumber =
            new PhoneNumberChooserTask();
         _choosePhoneNumber.Completed +=
            new EventHandler<PhoneNumberResult>(
               _choosePhoneNumber_Completed );
         SMS.Click +=
            new RoutedEventHandler( SMS_Click );
      }

      void SMS_Click(
         object sender,
         RoutedEventArgs e )
      {
         _choosePhoneNumber.Show();
      }

      void _choosePhoneNumber_Completed(
         object sender,
         PhoneNumberResult e )
      {
         _smsCompose = new SmsComposeTask();
         _smsCompose.Body =
            "Sending SMS message...";
         _smsCompose.To = e.PhoneNumber;
         _smsCompose.Show();
      }
   }
}
Share

About Jesse Liberty

Jesse Liberty is a Master Consultant for Falafel Software, and has three decades of experience writing and delivering software projects. He is the author of 2 dozen books and multiple Pluralsight courses, and has been a Technical Evangelist for Telerik and for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS.
This entry was posted in Patterns & Skills and tagged , . Bookmark the permalink.

3 Responses to Launchers & Choosers–Part 2

  1. Fast_Eddie says:

    Given you either compose/show either an SMS or Call, is there a way to pickup the result of the task? I’d like to know that the message/call was sent/placed and possibly how long the call lasted (etc..). Perhaps I can deduce this by saving values of when tombstoned and when app reactivates?

    Is there a way to pickup the “Name/Number” being dialed rather than just the phone number.

    If the user doesn’t complete task (e.g. presses cancel), I’d like my program to perhaps use in logic.

  2. Pingback: iPhone Developer? How to move to Windows Phone 7! | I Love Windows Phone!

  3. Pingback: Tweets that mention Launchers & Choosers

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>