Sunday, February 28, 2010

A weekend project with Android and GWT

For a long time, I thought of getting my hands dirty with Android and GWT (Google Web Toolkit) but never had a chance (part because I was lazy, I guess). Finally this weekend I took the entire two days to build an application. This post is about my experience with Android and GWT.

There is no specific reason to choose GWT apart from the fact that I have little to no knowledge in HTML, CSS and Java Script. If I were to use any other web frameworks out there for Java, like JSF, Spring MVC, etc I need to handle the web UI quirks separately. Where as in GWT it’s all handled by the framework itself. Yes I had to deal with some CSS, but got all my CSS from the sample applications that came with GWT.

This post is not about GWT, if you need more information check out here. However let me list out the benefits that I had by using GWT
  • It’s a component based model and hence you develop your entire UI in Java. Need not have to create a single piece of HTML code, apart from the home page.
  • The GWT compiler converts your UI code (which is written in Java as said above) into a high performance java script code. This will be used by the browser to render the UI you have built. If I were to create the UI with just HTML and Java Script I would have spent days on it (again this might not be the case for a HTML and CSS guru).
  • It comes with a great eclipse plugin, so you could just stay within eclipse to do all the GWT stuff. It comes with an integrated development server (based on Jetty) which you could use for quick testing.
  • GWT has it’s own RPC mechanism, under the hoods it’s all based on servlets just like any other web framework. However the key architecture difference here is that, GWT promotes the use of AJAX. The server communication is all based on AJAX. So you would only send and receive data rather than receiving the entire HTML page from the server.
  • Above all it has a great integration with Google API’s. I was using Google Map’s API and it was a breeze to get a Map displayed on the UI. Just two to three lines of code.

Android OS
I don’t have to talk much about what Android is as you might already know it, if not check out here. I was intrigued by the news earlier this week that Google now ships 60,000 android handsets per day (check here), so thought let me check out what it takes to build an application in Android.

The most obvious thing you would note about Android while reading about it, is that it’s a loosely coupled architecture and highly promotes reusability between components and for that matter between applications as well. I was not able to stop my self to compare it with Service Oriented / Messaging Architecture, will write more about it later. In Android one could build four types of components

  • Activities - for the UI
  • Services - does not require a UI, it runs in the background. Example a music player
  • Broadcast Receivers - these receive and react to system and other application broadcasts. Similar to messaging systems.
  • Content Providers - components that provide access to content in the phone, e.g. contacts data, media data, etc

My use case for this application is a very simple one, Whenever the phone boots up I need to start a back ground service which listens for requests from external source. External source being, in this case a web application running in an external server. Once a request is received the back ground service will send the current location data to the caller. The web application will then display it on a Google Maps mashup.

For the above use case to work I need only to concentrate on Broadcast Receivers and Services. My broad cast receiver will listen for system boot completed broadcast in Android OS. Once it receives the information that the phone is switched on, it starts the background service. My background Service starts a Server Socket and listens for client invocation. When it gets a client invocation, it uses the Location Manager API in Android and sends the current location data back to the caller.

All along in my career (which is just about 10 years), I worked in J2EE based applications and never had to deal with multi threading as the application server takes care of it. In Android, one has to deal with multi threading if the application does a lot of background processes. Overall I learnt a lot in the process of building the above application. I also made so many mistakes that every beginner makes though there are good documentation at the developer site. In the days to come, I will post about these beginner mistakes and the lessons that I have learnt in detail.

No comments:

Post a Comment