How read a conversation using Gmail.java
I need to read gmail messages/conversation to make a program for blind people. I'm using Gmail.java to access gmail database. The problem its that i dont know how to access to the body of the mails.
Anyone can help me?
A small excerpt of my code:
Uri uri = Uri.parse( Gmail.AUTHORITY_PLUS_CONVERSATIONS + account + "/"); Cursor cursorConversacion = contentResolver.query( uri, Gmail.CONVERSATION_PROJECTION, null, null, null);
int j = 1;
//Comprobamos que hay cursor
if (cursorConversacion.moveToFirst()){
Gmail gmail = new Gmail(mContext.getContentResolver());
ConversationCursor cc = new ConversationCursor(gmail,account,cursorConversacion);
do{
String cuenta = cc.getAccount();
String snippet = cc.getFromSnippetInstructions();
String subject = cc.getSubject();
String numMessages = "" + cc.getNumMessages();
String conversationID = "" + cc.getConversationId();
Log.d("DEBUG","\n\n\n MENSAJES " + j + " \n\n" +
" ID: " + conversationID +
" Num messages: " + numMessages +
" Subject: " + subject +
"\nCuenta: " + cuenta +
" snippet: " + snippet
);
//MessageCursor a partir de la conversacion
Uri uriMessages = Uri.parse(
Gmail.AUTHORITY_PLUS_CONVERSATIONS + account + "/" + conversationID + "/messages");
Cursor cursorMessages = contentResolver.query(
uri, Gmail.MESSAGE_PROJECTION, null, null, null);
if (cursorMessages.moveToFirst()){
int k=1;
do{
MessageCursor mc = new MessageCursor(gmail, contentResolver, account, cursorMessages);
Log.d("DEBUG","Mensaje " + k + " " + mc.getBody());
k++;
}while ( (k < 10) && (cursorMessages.moveToNext()) ) ;
}
j++;
}while ( (j< 20) && (cursorConversacion.moveToNext()) );
}
The problem is that i don’t know how to access to the mail’s body.
The access to conversation works but the messageCursor doesn’t. I know/believe that the use of messageCursor isn't for message(a message of a conversation), and i know tha开发者_如何转开发t the use of undecommented provider is a bad idea (if you have a better one tell me, please!!!!) but it is the better solution i found to solve my problem.
Ty for help, and sorry for my english.
Autoanswer :D
public void getMails(Context mContext){
String account="blablabla_accountgmail@gmail.com";
ContentResolver contentResolver= mContext.getContentResolver();
Cursor cursor = contentResolver.query(ACCOUNT_URI, null, null, null, null);
Uri uri = Uri.parse(
Gmail.AUTHORITY_PLUS_CONVERSATIONS + account + "/");
Cursor cursorConversacion = contentResolver.query(
uri, Gmail.CONVERSATION_PROJECTION, null, null, null);
int j = 1;
//check cursor
if (cursorConversacion.moveToFirst()){
Gmail gmail = new Gmail(mContext.getContentResolver());
ConversationCursor cc = new ConversationCursor(gmail,account,cursorConversacion);
do{ //extract some data for the example
String cuenta = cc.getAccount();
String snippet = cc.getFromSnippetInstructions();
String subject = cc.getSubject();
String numMessages = "" + cc.getNumMessages();
String conversationID = "" + cc.getConversationId();
Log.d("DEBUG","\n\n\n MENSAJES " + j + " \n\n" +
" ID: " + conversationID +
" Num messages: " + numMessages +
" Subject: " + subject +
"\nCuenta: " + cuenta +
" snippet: " + snippet
);
MessageCursor mc = new Gmail(contentResolver).getMessageCursorForConversationId(account, cc.getConversationId());
if (mc.getCursor().moveToFirst()){
int k=1;
do{
//MessageCursor mc = new MessageCursor(gmail, contentResolver, account, cursorMessages);
Log.d("DEBUG","Mensaje " + k + " " + mc.getBody());
Log.d("DEBUG",mc.getFromAddress());
k++;
}while ( (k < 10) && (mc.next()) ) ;
}
j++;
}while ( (j< 20) && (cursorConversacion.moveToNext()) );
}
THIS CODE USES Gmail.java (link in my question post) and it's an example to extract the body of an emails. You can extract other attributes like mailFrom or date send
精彩评论