Tuesday, July 16, 2013

Windows Phone Random Dialer Demo

This is posted on day two of the annual CSTA Conference. This afternoon I am taking part in a mobile developer throw down. Three of us are writing the same program on different platforms: iOS, Android and Windows Phone. Guess which one I am doing? Yep, Windows Phone. The challenge is simple. Write an app that picks a  random contact from your phone’s list of contacts and then gives you the option to dial it.
Obviously I have been practicing. In fact I recorded some of my practices. Since time is short for the throw down and I will have to talk fast I decided I should make both the code and the demo available via this blog. The first, less than 3 minutes video, is of using Visual Studio 2012 to create the very simple user interface.
Creating a simple UI for a phone dialer app
No code in that part. Just drag and draw a button and two textblocks. Do a little setting of parameters but not much more. Obviously in a “real” app there would be a lot more. Next though is writing the code behind the app. Here is that video (about 10 minutes 30 seconds).
Writing the code
First we make sure we include some useful libraries. Specifically the ones for accessing user data and phone tasks.
using Microsoft.Phone.UserData;
using Microsoft.Phone.Tasks;


Next we create some code to respond to a click on the button. Not the ideal Windows phone way to do it but hey this is just a demo.


private void btnFind_Click(object sender, RoutedEventArgs e)
{
    Contacts cons = new Contacts();

   cons.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(Contacts_SearchCompleted);

  cons.SearchAsync(String.Empty, FilterKind.None, "Contacts test #1");
}



The Contacts type is a collection of contacts. We’re setting up Contacts_SearchCompleted to be an event handler to execute when our asynchronous search completes. Obviously Contacts.SearchAsync starts our search. We want the search to run asynchronously so as to prevent the phone from getting stuck. We want other apps in the background to be able to do their thing. So what does the event handler do?


 void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
        {
            Contact c;
            int count = e.Results.Count();
            int tryMe;
            Random r = new Random();

            do
            {
                tryMe = r.Next(0, count);
                c = e.Results.ElementAt(tryMe);
            } while (c.PhoneNumbers.Count() < 1);

            txtName.Text = c.DisplayName;
            txtPhone.Text = c.PhoneNumbers.ElementAt(0).ToString();

            PhoneCallTask tsk = new PhoneCallTask();
            tsk.PhoneNumber = txtPhone.Text;
            tsk.Show();


        }


We’ve got some variables there. Since I have some contacts on my phone that don’t have phone numbers I wanted to make sure I only tried to call people with phone numbers. So after randomly selecting a contact I check to make sure the count of phone numbers is at least one. As long as I eventually find a phone number the code displays the name and number in textblocks. Lastly the code creates a phone call task, assigns the task a number and executes the call. The call task does ask if you want to call or cancel the call.

So there you have it. Not beautiful but just enough code to show you how to create a very simple app using the available phone libraries.

A video of the live session including the iOS and App Inventor for Android development is available at http://youtu.be/AD7PRDmYfaQ

No comments: