Search code examples
androidemailnotificationsgmailbroadcastreceiver

How to detect that i have received an email/gmail on my Android Device?


Is there a way to detect or an event being triggered when i receive a gmail/email on my android device.

Basically i would like my application to read the email notification when i receive it.

Could you kindly tell me if there is way to do this ?


Solution

  • You need to register a content observer (not broadcast receiver)

    contentResolver.registerContentObserver(Uri.parse("content://gmail-ls"), true, gmailObserver); 
    

    gmailObserver is your own ContentObserver object.

    ContentObserver.onChange is going to be called every time something changes in Gmail. Here you get all conversations like so:

    Cursor conversations = _contentResolver.query(Uri.parse("content://gmail-ls/conversations/" + YourEmailAddress), null, null, null, null); 
    

    And the actual conversation messages will be:

    Cursor messages = _contentResolver.query(Uri.parse("content://gmail-ls/conversations/" + YourEmailAddress + "/" + String.valueOf(conversationId) + "/messages"), null, null, null, null);