Mobile Services is an exciting feature which makes it pretty easy to create highly-functional mobile apps using Windows Azure.
By this service, you are able to create a scalable and secure backend for Windows, Android, and iOS apps, or application working with HTML/JavaScript. It can store data in the cloud in the format of Table. In addition, It can connect to notification server (such as GCM for Android) to implement Pushing of mobile. It can even allow you to easily authenticate users with Facebook, Twitter, Microsoft, or Google account. I am impressed much because these abilities just need a few lines.
This article tells more:
Windows Azure Mobile Services - MSDN – Microsoft
These days, I took some time to practise with Mobile Services and created a small demo. Here is a short description about how the demo works:
- the Android client draws some graphics (for simplicity, only lines) and upload the point data to the Mobile Services.
- the desktop client (HTML) gets the data and draws the graphics in AutoCAD. As you know, AutoCAD 2014 supports JavaScript. We could benefit from the new feature.
In this section, I will describe the Android client. The source project is available here: Download Mydrawtest
Firstly, you need to create a Mobile Service in Azure and the companion SQL database. This article introduces the detailed steps:
http://www.windowsazure.com/en-us/develop/mobile/tutorials/get-started/
Next, create the table. The table name will be the class name we defined in the client application. e.g. in the snapshot below, I created 3 tables. AzureTableItem is what will be used in the Android client of this sample. You can browser the existing records in Azure.
After the service is ready, you can start to work with the client applications. Azure provides the cool stuffs for the developers of various kinds of applications. In each type, you could have two options to start:
- [Create A New Android App ]: create & download the demo project of Android
- [Connect an Existing Android App]: a short guide on how to work with Android application.
I’d recommend with the former if you are not much confident.
I created an Eclipse application of Android, add the necessary libraries of Mobile Service. The article below tells the detailed steps:
http://www.windowsazure.com/en-us/develop/mobile/how-to-guides/work-with-android-client-library/
Here is some snippet of the Android project:
1. Main view for drawing: get touching point and draw lines in the canvas.
public class MyView extends View {
// drawing properties
Paint paint = new Paint();
//point array
public static ArrayList<MyPoint> arrList =
new ArrayList<MyPoint>();
public MyView(Context context) {
super(context);
//set background color to green
this.setBackgroundColor(Color.GREEN);
//set the pen width
paint.setStrokeWidth(8);
// set pen color
paint.setColor(Color.RED);
}
// draw the lines in canvas
@Override
public void onDraw(Canvas canvas) {
if(arrList.size() < 2)
return;
for(int i = 0; i< arrList.size() -1 ; i++)
{
canvas.drawLine(arrList.get(i).getX(),
arrList.get(i ).getY(),
arrList.get(i + 1).getX(),
arrList.get(i + 1 ).getY(),
paint);
}
}
// get the touch point
// add it to the array of points
@Override
public boolean onTouchEvent(MotionEvent e) {
float x = e.getX();
float y = e.getY();
switch (e.getAction()) {
case MotionEvent.ACTION_DOWN :
MyPoint oNewPt = new MyPoint(x,y);
arrList.add(oNewPt);
}
this.invalidate();
return true;
}
}
2. AzureTableItem: the class of the table. Note: the serialize name cannot contain space, otherwise you will get an exception "error while processing request".
public class AzureTableItem {
//Item Id
@com.google.gson.annotations.SerializedName("ID")
public int id;
//Item graphics data.
@com.google.gson.annotations.SerializedName("GraphicsData")
public String oGraphStr;
}
3. Main activity: main activity to upload the graphics data.
3.1) Connect to the mobile service.
//connect to mobile service
try {
mClient = new MobileServiceClient(
"Your Mobile Service Site",
"Your Access Key",
this
);
//get the table
mMobileTable = mClient.getTable(AzureTableItem.class);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
3.2) upload the graphics data.
if(mClient != null)
{
// create a new item of graphics
AzureTableItem oATI = new AzureTableItem();
//convert the graphics points to json string
Gson gson = new Gson();
String json = gson.toJson(mMyView.arrList);
oATI.oGraphStr = json;
// Insert the new item
mMobileTable.insert(oATI,
new TableOperationCallback<AzureTableItem>() {
public void onCompleted(AzureTableItem entity, Exception exception, ServiceFilterResponse response) {
if (exception == null) {
createAndShowDialog("The new item is uploaded",
"Succeed!");
}
else {
createAndShowDialog(exception, "Error");
}
}
});
}
Run the application, touch the screen to draw some lines.click option menus and upload the graphics to Azure. A new record will generate.
In the next section, I will describe the desktop client (HTML).
Comments