I have ported the cometd java client from Jetty to run on the google android phone. Cometd implements the bayeux protocol for push messaging over HTTP and is normally used by Ajax clients running in the browser. By porting the java client to android, this allows native applications on the phone to have bidirectional messaging to a server over HTTP.
To demonstrate this, I’ve implemented a simple chat room that interacts with the Ajax Comet chat room demos. This would be a great basis for other android applications that need to update in realtime.
The following core dependencies were used:
- jetty-client-6.1.12.rc2 (asynchronous http client)
- bayeux-client-6.1.12.rc2 (bayeux protocol via http)
I had been wrestling with android for a couple of days trying to get familiar with their apis. The more you dive into it, you would notice a pattern that you might be used to, which is developing webapps using the MVC pattern .
To get started, you can use the ADT (Android Developer Tools) eclipse plugin. Although it could speed up your productivity, it would can up your cpu and memory as well. It takes a few seconds to recompile(annoying) even with just the slightest change in code.
import part of the plugin is the auto-update of the R.class of your
application (after adding/editing/removing your views and other xml
Implementing the chat client using cometd-jetty client was a breeze because all the work (bayex protocol) was already done by org.mortbay.cometd.client.BayeuxClient.
The one problem I’ve encountered was updating android’s UI from another thread (e.g message received event). If I had read all the android FAQs, I wouldn’t have been stuck on it. Oh well …so the solution was simply creating an android.os.Handler inside your Activity to be able to receive asynchronous events (dispatched from another thread) and to be able to render/update your UI.
public class MyActivity extends Activity
public static final int SOME_EVENT = 10;
Handler _handler = new Handler()
public void handleMessage(android.os.Message msg)
// update your ui here
Runnable _someEventHandler = new Runnable()
public void run()
The source and apk is available here.
To chat live (after installation), change your connection settings: