Get started with Mobile Services
This tutorial shows you how to add a cloud-based backend service to an Android app using Azure Mobile Services. In this tutorial, you will create both a new mobile service and a simple To do list app that stores app data in the new mobile service.
A screenshot from the completed app is below:
Completing this tutorial requires the Android Developer Tools, which includes the Android Studio integrated development environment, and the latest Android platform. Android 4.2 or a later version is required.
The downloaded quickstart project contains the Azure Mobile Services SDK for Android.
To complete this tutorial, you need an Azure account. If you don't have an account, you can create a free trial account in just a couple of minutes. For details, see Azure Free Trial.
If you would like to see the source code of the completed app, go here.
Create a new mobile service
Follow these steps to create a new mobile service.
Log into the Management Portal. At the bottom of the navigation pane, click +NEW. Expand Compute and Mobile Service, then click Create.
This displays the Create a Mobile Service dialog.
This displays the Specify database settings page. >[AZURE.NOTE]As part of this tutorial, you create a new SQL Database instance and server. You can reuse this new database and administer it as you would any other SQL Database instance. If you already have a database in the same region as the new mobile service, you can instead choose Use existing Database and then select that database. The use of a database in a different region is not recommended because of additional bandwidth costs and higher latencies.
In Name, type the name of the new database, then type Login name, which is the administrator login name for the new SQL Database server, type and confirm the password, and click the check button to complete the process.
You have now created a new mobile service that can be used by your mobile apps.
Create a new Android app
Once you have created your mobile service, you can follow an easy quickstart in the Management Portal to either create a new app or modify an existing app to connect to your mobile service.
In this section you will create a new Android app that is connected to your mobile service.
In the Management Portal, click Mobile Services, and then click the mobile service that you just created.
In the quickstart tab, click Android under Choose platform and expand Create a new Android app.
This displays the three easy steps to create an Android app connected to your mobile service.
If you haven't already done so, download and install the Android Developer Tools on your local computer or virtual machine.
Click Create TodoItem table to create a table to store app data.
Now download your app:
- The latest app version uses the Mobile Services Android SDK 2.0. You can download that version from here. Click Download Zip, unzip it, and the project is in GettingStarted under the Android folder.
- An earlier version uses the prior version of the SDK. To use it, under Download and run your app, click Download. This downloads the project for the sample To do list application that is connected to your mobile service. The project files are compressed, so browse to their location and expand the files on your computer.
Run your Android app
The final stage of this tutorial is to build and run your new app.
Browse to the location where you saved the compressed project files and expand the files on your computer into your Android Studio projects directory.
Open Android Studio. If you are working with a project and it appears, close the project (File => Close Project).
Select Open an existing Android Studio project, browse to the project location, and then click OK.
In the left Project Explorer window, ensure that the Project tab is selected, then open app, src, java and double click on ToDoactivity,
If you downloaded version 2.0 of the SDK, you need to update the code with the Url and key of your mobile service:
- Find the OnCreate method in TodoActivity.java and locate the code that instantiates the mobile services client. The code is visible in the preceding image.
- Replace "MobileServiceUrl" with the actual Url of your mobile service.
- Replace "AppKey" with the key of your mobile service.
- For more details consult the tutorial Add Mobile Services to an existing app.
From the Run menu, click Run to start the project in the Android emulator.
To be able to run the project in the Android emulator, you must define a least one Android Virtual Device (AVD). Use the AVD Manager to create and manage these devices.
In the app, type meaningful text, such as Complete the tutorial, and then click Add.
This sends a POST request to the new mobile service hosted in Azure. Data from the request is inserted into the TodoItem table. Items stored in the table are returned by the mobile service, and the data is displayed in the list.
You can review the code that accesses your mobile service to query and insert data, which is found in the ToDoActivity.java file.
Back in the Management Portal, click the Data tab and then click the TodoItems table.
This lets you browse the data inserted by the app into the table.
Now that you have completed the quickstart, learn how to perform additional important tasks in Mobile Services: