Sunday, July 7, 2013

The Android+Azure Love Story

Once upon a time, there was this beautiful princess named ‘Android’. ‘Azure’ was the most powerful warrior at that time. One sunny day… Ok, let’s cut the crap and get down to business.

If you haven’t tried out Windows Azure yet, well then you’ve been missing out a lot. Windows Azure is amazing! If you’ve been living under a rock for the last couple of years, Azure is Microsoft’s flexible, open & rock solid cloud platform. And by any chance if you haven’t heard of Android, stop reading right now and go kill yourself!

With the introduction of the Mobile Services SDK for Android, Windows Azure supports developers with super easy access to structure data storage, authenticate and push notifications using GCM. Now you are going to learn how to get started.

Prerequisites:

Let’s build a simple ‘Contacts’ app which saves its content on the cloud. Sounds pretty lame, but we gotta start somewhere right?

Step 1: Create a new mobile service in Azure.


Step 2: Go to the DATA tab and create a new table named ‘Contact’. You do not need to create the columns. They are automatically generated when the data is inserted.


Step 3: Create a new Android project.

Step 4: Extract the .jar files in Mobile Services SDK to the project’s lib folder.

Step 5: Create a new class to hold the contact data.


The class name has to be the same name as of the table in mobile service database. Case doesn't matter.


Did you notice the annotations? They specify the mapping between the DTO fields and the back end Azure table columns. They even work with private fields.

Step 6: Modify onCreate method of your main activity to insert a random contact.


Client creation takes the mobile service URL and the application key, which can be found in the portal.

Note: `getRandomContact’ and  ‘showMessage’ method implementation details are omitted for brevity.

Step 7: Run the app couple of times.


Step 8: Browse the Contact table in Azure.


So there you have it. An Android app hookup up with an Azure backend in no time! And that’s not all; you can perform all your CRUD operations with ease. You can even intercept requests to the database using the ‘Script’ tab. This is particularly useful for hooking up push notifications for the GCM clients, like illustrated below.


The only thing you have to do is to enter your API key in the ‘Push’ tab. You can get your API key by registering the app in Google APIs console.

Also you can setup authentication for your app using Google, Facebook, Microsoft or Twitter accounts very easily. All you gotta do is add the credentials under the ‘Identity’ tab.

So, hopefully you figured how easy it is to use Azure mobile services with Android. Remember that you get 10 free mobile services with your free trial, so start experimenting now. I’m sure you are going to love it.

Inspired by a Colombo Mobile Developer Meetup session.

No comments:

Post a Comment