How do the retrieve the date of the mms from conte

2019-03-02 17:35发布

问题:

I did get the information on how to retrieve the text and the image for the mms sent from this link: How to Read MMS Data in Android?.

But I am not sure how to retrieve the date for the mms that was sent.

I know I have to look into content://mms and not in content://mms/part.

This is the Mothod to retrieve the mms text:

private String getMmsText(String id) {
        Uri partURI = Uri.parse("content://mms/part/" + id);
        InputStream is = null;
        StringBuilder sb = new StringBuilder();
        try {
            is = 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();
    }

and then, in the onCreate method, I use this code to get the info:

Cursor cursor = getContentResolver().query(uri, null, selectionPart,
                null, null);
        if (cursor.moveToFirst()) {
            do {
                String partId = cursor.getString(cursor.getColumnIndex("_id"));
                String type = cursor.getString(cursor.getColumnIndex("ct"));
                if ("text/plain".equals(type)) {
                    String data = cursor.getString(cursor
                            .getColumnIndex("_data"));

                    if (data != null) {
                        // implementation of this method above
                        body = getMmsText(partId);
                    } else {
                        body = cursor.getString(cursor.getColumnIndex("text"));
                    }
                }
            } while (cursor.moveToNext());
        }

        try {


            main.setText(body);
            img.setImageBitmap(bitmap);
        } catch (Exception e) {

            e.printStackTrace();
        }

I just want to know where can I make changes to get the date value.

Some info will be really helpful.

回答1:

I'm not overly familiar with MMS's, but I'd imagine something like this would at least get you started

Cursor cursor = activity.getContentResolver().query(Uri.parse("content://mms"),null,null,null,date DESC);
count = cursor.getCount();
if (count > 0) 
{
    cursor.moveToFirst();
    long timestamp = cursor.getLong(2);
    Date date = new Date(timestamp);
    String subject = cursor.getString(3);
}

It's completely untested of course, but should point you in the right direction. Hope this helps!

Edit After doing a bit of reading, there used to be (possibly still is) a "bug" with the timestamp in MMS messages, when retrieving the data. If you end up with a silly value (like the epoch), you'll have to * 1000 before using it. Just an aside :) I.e.:

long timestamp = (cursor.getLong(2) * 1000);