Scott Allen Cambo
Scott Allen Cambo

Reputation: 11

Second most recent MMS received instead of most recent in Android

Recently, I have been working on an Android project that requires me to get the text from and MMS. I don't have a whole lot of experience with Android, but I am familiar with SQL and java and I have been following these posts as closely as possible.

The code that I came up with mostly works, but it seems to always grab the text from the second most recent MMS message and not the most recent one. Any idea why this would be happening?

Here are the relevant parts of the MMSReceiver class that I have made using the two threads mentioned earlier as a guide.

First, is the onReceive method... public class MMSReceiver extends BroadcastReceiver {

private static final String ACTION_MMS_RECEIVED = "android.provider.Telephony.WAP_PUSH_RECEIVED";
private static final String MMS_DATA_TYPE = "application/vnd.wap.mms-message";
private static String message;

@Override
public void onReceive(Context context, Intent intent) {
    Log.d("Test" , "MMSReceiver!");
    Uri uri = Uri.parse("content://mms-sms/conversations");     
    String action = intent.getAction();
    String type = intent.getType();
    //Log.d("MMS", "action is " + action + " , type is " + type);
    final Context cont = context;
    ContentResolver cr = context.getContentResolver();
    final String[] projection = new String[]{"*"};
    Uri MMSuri = Uri.parse("content://mms/inbox/");
    Cursor MmsIDquery = cr.query(uri, projection, null, null, null);
    MmsIDquery.moveToFirst();
    String mid = MmsIDquery.getString(MmsIDquery.getColumnIndex("_id"));
    Log.d("MMS", "message id for unreadable message is " + mid);
    message = getMessageText(cont, mid);
    ...

The getMessageText method is passed the context and the message id and is written as this...

    /* gets message text */
public String getMessageText(Context context, String mmsid){
    Log.d("MMS", "mmsid was " + mmsid);
    String message = null;
    String mid = ""+(Integer.parseInt(mmsid)+2);
    Log.d("MMS", "mmsid is now " + mid);
    String selectionPart = "mid=" + mmsid;
    Uri mmsTextUri = Uri.parse("content://mms/part");
    Cursor cursor = context.getContentResolver().query(mmsTextUri, null, selectionPart, null, null);
    if (cursor.moveToFirst()){
        do{
            String partId = cursor.getString(cursor.getColumnIndex("_id"));
            String type = cursor.getString(cursor.getColumnIndex("ct"));
            Log.d("MMS", "getMessageText was called, partId = " + partId + " , type = " + type);
            if ("text/plain".equals(type)){
                String data = cursor.getString(cursor.getColumnIndex("_data"));
                Log.d("MMS", "data was " + data);
                if (data != null){
                    message = getMmsText(context, partId);
                    Log.d("MMS", "body was " + message);
                } else {
                    message = cursor.getString(cursor.getColumnIndex("text"));
                    Log.d("MMS", "body was " + message);
                }
            }
        } while (cursor.moveToNext());
    } else {
        Log.d("MMS", "Query returned nothing in getMessageText()");
    }
    return message;
}

Within this method, the getMmsText is called. I realize that this process is a bit redundant, but I was having a hard time understanding all of what goes on during this process so I kept it as similar to the original threads as I could in order to be sure that the problem wasn't in the way that I reduced it for my own code.

getMmsText looks like this...

    public String getMmsText(Context c, String id){
    Log.d("MMS", "getMmsText was called with " + id);
    Uri partUri = Uri.parse("content://mms/inbox/" + id);
    InputStream is = null;
    StringBuilder sb = new StringBuilder();
    try{
        is = c.getContentResolver().openInputStream(partUri);
        if (is != null){
            InputStreamReader isr = new InputStreamReader(is, "UTF-8");
            BufferedReader reader = new BufferedReader(isr);
            String temp = reader.readLine();
            while(temp != null){
                sb.append(temp);
                temp = reader.readLine();
            }
        }
    } catch (IOException e){}
    finally {
        if (is != null){
            try{
                is.close();
            } catch (IOException e){}
        }
    }
    return sb.toString();
}

Thank you so much for your help and please let me know if you have any questions that can help with your answering.

Upvotes: 1

Views: 630

Answers (1)

Christian Ramos
Christian Ramos

Reputation: 84

I, myself, have not done the whole process exactly but am currently researching it and this is what I have learned thus far. I recall reading before that listening for android.provider.Telephony.WAP_PUSH_RECEIVED will only notify you of an incoming MMS. My understanding is that the intent is more of a receipt and does not actually mean the MMS is in the phone yet, or more precisely, in the database to pull from. That may explain why you are only getting the second to last. If you want to get the MMS when it's finished and ready to be pulled from the database, I think you'll need a ContentObserver to notify you of database updates.

Upvotes: 1

Related Questions