4

I've spent 5 days trying out different things and lots of googling with no luck

I have a broadcast receiver to monitor and backup incoming mms & sms. The sms - outgoing and incoming is easy no problem. MMS however...

I have a broadcast receiver for incoming MMS, no problem there.

For outgoing MMS however, I use a content observer directed towards content://mms

heres the part registering the content observer from the service class

mo = new MMSObserver(new Handler(),getApplicationContext());
        try{
            getContentResolver().unregisterContentObserver(mo);
        }
        finally{
            getContentResolver().registerContentObserver(Uri.parse("content://mms"), true, mo);
        }

This is the onchange part in the above content observer

public void onChange(boolean bSelfChange)
{
    super.onChange(bSelfChange);
    Log.i(TAG,"MMSObserver onChange");
    ContentResolver contentResolver = context.getContentResolver();
    Uri uri = Uri.parse("content://mms");
    Cursor cur = contentResolver.quert("content://mms",null,null,null,null)
    if(cur.moveToNext()){
        String id = cur.getString(cur.getColumnIndex("_id"));
        String date = cur.getString (cur.getColumnIndex ("date"));
            String address = getAddress(id);
    }
}
    private static String getAddress(String id){
          String selectionAdd = new String("msg_id=" + id);
            String uriStr = MessageFormat.format("content://mms/{0}/addr", id);
            Uri uriAddress = Uri.parse(uriStr);
            Cursor cAdd = context.getContentResolver().query(uriAddress, null,
                selectionAdd, null, null);
            String name = null;
            if (cAdd.moveToFirst()) {
                do {
                    String number = cAdd.getString(cAdd.getColumnIndex("address"));
                    if (number != null) {
                        try {
                            Long.parseLong(number.replace("-", ""));
                            name = number;
                        } catch (NumberFormatException nfe) {
                            if (name == null) {
                                name = number;
                            }
                        }
                    }
                } while (cAdd.moveToNext());
            }
            if (cAdd != null) {
                cAdd.close();
            }
            return name;
    }

The problem is the address column always returns "insert-address-token" for outgoing mms. Is there any possible way to get the number the mms is going to?

Also I noticed that the content observer is triggered when the message is in draft form not when it is sent or pending. since depending on those uris is generally a bad idea since they're not part of the sdk, i switched to a different method. cataloging all sms & mms messages and storing their _id columns, and just syncing them with the backup. However my problem still remains.

MMS address column is always "insert-address-token"

Any suggestions?

Matt
  • 22,721
  • 17
  • 71
  • 112
jitcoder
  • 41
  • 1
  • 2
  • What is the `type` value of the `insert-address-token` - it should be `137` - but it *should* also be accompanied by a type `151` containing the actual `address` value. You only get a single row? – Jens Apr 30 '12 at 08:33

1 Answers1

1

You may want to try something like this:

Uri uri = Uri.parse("content://mms-sms/conversations/" + mThreadId);
String[] projection = new String[] {
    "body", "person", "sub", "subject", "retr_st", "type", "date", "ct_cls", "sub_cs", "_id", "read", "ct_l", "st", "msg_box", "reply_path_present", "m_cls", "read_status", "ct_t", "status", "retr_txt_cs", "d_rpt", "error_code", "m_id", "date_sent", "m_type", "v", "exp", "pri", "service_center", "address", "rr", "rpt_a", "resp_txt", "locked", "resp_st", "m_size"
};
String sortOrder = "normalized_date";

Cursor mCursor = getActivity().getContentResolver().query(uri, projection, null, null, sortOrder);

String messageAddress;
int type;
while (mCursor.moveToNext()) {
    String messageId = mCursor.getString(mCursor.getColumnIndex("_id"));

    Uri.Builder builder = Uri.parse("content://mms").buildUpon();
    builder.appendPath(messageId).appendPath("addr");
    Cursor c = mContext.getContentResolver().query(builder.build(), new String[] {
        "*"
    }, null, null, null);
    while (c.moveToNext()) {
        messageAddress = c.getString(c.getColumnIndex("address"));

        if (!messageAddress.equals("insert-address-token")) {
            type = c.getInt(c.getColumnIndex("type"));
            c.moveToLast();
        }
    }
    c.close();
}

I am not actually calling the mCursor's moveToNext() method in my code but instead I am implementing this logic in the getView() method of a SimpleCursorAdapter.

Etienne Lawlor
  • 6,817
  • 18
  • 77
  • 89
  • This is unsafe, not all the fields in your `projection` are present in all versions of android, in any case you don't need them all. – vikki Feb 19 '13 at 06:10
  • although I should point out that if OP is looking to backup the messages then it's much safer to use a null protection and just save the entire db as is. – vikki Feb 19 '13 at 06:17