Getting direct access to the actual message is impossible, as far as I know. The best way I know is to use the Java MAIL library for Android ( http://code.google.com/p/javamail-android/ ) and use the IMAP protocol to capture the message you are looking for.
But your actual question was about getting events from gmail. To get them, you can use ContentObserver. First declare your own subclass of ContentObserver:
class GmailContentObserver extends ContentObserver { public GmailContentObserver(Handler handler) { super(handler); } @Override public void onChange(boolean selfChange) { onChange(selfChange, null); } @Override public void onChange(boolean selfChange, Uri uri) {
Then register your ContentObserver:
GmailContentObserver gco = new GmailContentObserver(new Handler()); ContentResolver cr = getContentResolver(); cr.registerContentObserver(Uri.parse("content://gmail-ls"),true,gco);
What is it!
Please note that you must also unregister your observer when you no longer need it, or when your application / service shuts down.
Also note new Hanlder() when instantiating your observer. This will determine which thread the onChange methods will be called on. The thread on which the Handler object was created is the one that will call onChange (in my example, you cannot see this, but this is the UI thread, the one that calls onCreate in my activity).
When registering your observer, there are two parameters: the URI that you want to watch, and whether you want to watch the descendants. In the above code example, you will receive a notification for the URI content://gmail-ls , but also, for example, for content://gmail-ls/ foo@gmail.com , foo@gmail.com , which is your gmail account. And there are many more child URIs (especially labels). To get fewer events, you can register your observer for content://gmail-ls/ foo@gmail.com and set the second parameter to false.
Given this mechanism, theoretically, if you want to observe changes in a given Gmail tag, you can request a tag identifier (see gmail api) and register the corresponding URI content://gmail-ls/ foo@gmail.com /label/80 , 80 - tag identifier. But for some reason this does not work.
Also check the already mentioned gmail api for code to determine if the installed gmail application has a content provider (depending on the gmail version).