How do the retrieve the date of the mms from content://mms.

淺唱寂寞╮ 提交于 2019-12-02 07:37:50

问题


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);


来源:https://stackoverflow.com/questions/21460486/how-do-the-retrieve-the-date-of-the-mms-from-content-mms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!