【android】 簡訊多媒體訊息以及會話讀取

來源:互聯網
上載者:User

一、需要實現一個同系統資訊一樣的功能

1)會話列表;

2)會話對話詳情;

3)系統會話增加新資訊或者刪除資訊等變化時做到同步;

二、實現思路

通過查詢會話表顯示會話介面,監聽會話資料庫實現與系統資訊同步。通過會話id查詢會話對應的具體聊天內容,比如簡訊或者多媒體訊息;

看下mmssms.db中的uri有好多個,可以根據自己的需求選擇合適的uri,可以避免不必要的操作,提高效率。經過多次測試,實現以上功能需要用的資訊表有:

1)會話表:content://mms-sms/conversations/

2)簡訊表:content://sms

3)多媒體訊息表:content://mms、content://mms/part

4)資訊對應手機號碼錶:content://mms-sms/canonical-addresses

需要許可權:<uses-permission android:name="android.permission.READ_SMS" />

首先:顯示會話列表

public static final Uri THREADS_URI = Uri.parse("content://mms-sms/conversations?simple=true");

threads欄位:

_id
date
ct_t:區分多媒體訊息還是簡訊:application/vnd.wap.multipart.related是多媒體訊息,否則是簡訊
snippet:最新的一條會話資訊。多媒體訊息為多媒體訊息的主題,簡訊時簡訊的body
recipient_ids:資訊對應手機號碼錶(content://mms-sms/canonical-addresses)中的id,查詢對應的手機號碼

message_count:詳細對話條數

read

snippet_cs:snippet的編碼方式,多媒體訊息:106(utf-8),簡訊:0

……

根據threads表中的date,snippet,messagecount基本可以顯示會話列表了。

另外:

1:通過4)資訊對應手機號碼錶:content://mms-sms/canonical-addresses擷取了手機號之後擷取連絡人姓名以及頭像使用:ContactsContract.CommonDataKinds.Phone.CONTENT_URI

2:當會話為多媒體訊息時,snippet顯示的是多媒體訊息的主題,編碼方式為utf-8。顯示為亂碼。

解決:snippet_mms = new String(snippet.getBytes("ISO8859_1"), "utf-8");

其次,擷取會話具體對話內容

具體對話的格式有ssm或者mms,通過content://mms-sms/conversations/threadid,可以查詢與某連絡人的所有聊天記錄的id,這個id對應簡訊或者多媒體訊息表中的id。如果具體會話是簡訊,則去sms表中查詢具體內容,如果是多媒體訊息則去mms表中查詢。通過欄位ct_t 區分資訊類別。

具體實現:

1)擷取sms

String selection = "_id = "+id;Uri uri = Uri.parse("content://sms");Cursor cursor = contentResolver.query(uri, null, selection, null, null);String phone = cursor.getString(cursor.getColumnIndex("address"));int type = cursor.getInt(cursor.getColumnIndex("type"));// 2 = sent, etc.String date = cursor.getString(cursor.getColumnIndex("date"));String body = cursor.getString(cursor.getColumnIndex("body"));

2)擷取mms

多媒體訊息表:content://mms

多媒體訊息附件:content://mms/part/

get text content from MMS:
String selectionPart = "mid=" + mmsId;Uri uri = Uri.parse("content://mms/part");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(cPart.getColumnIndex("_data"));            String body;            if (data != null) {                // implementation of this method below                body = getMmsText(partId);            } else {                body = cursor.getString(cursor.getColumnIndex("text"));            }        }    } while (cursor.moveToNext());}
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();}

擷取多媒體訊息中圖片:

String selectionPart = "mid=" + mmsId;Uri uri = Uri.parse("content://mms/part");Cursor cPart = getContentResolver().query(uri, null,    selectionPart, null, null);if (cPart.moveToFirst()) {    do {        String partId = cPart.getString(cPart.getColumnIndex("_id"));        String type = cPart.getString(cPart.getColumnIndex("ct"));        if ("image/jpeg".equals(type) || "image/bmp".equals(type) ||                "image/gif".equals(type) || "image/jpg".equals(type) ||                "image/png".equals(type)) {            Bitmap bitmap = getMmsImage(partId);        }    } while (cPart.moveToNext());}

private Bitmap getMmsImage(String _id) {    Uri partURI = Uri.parse("content://mms/part/" + _id);    InputStream is = null;    Bitmap bitmap = null;    try {        is = getContentResolver().openInputStream(partURI);        bitmap = BitmapFactory.decodeStream(is);    } catch (IOException e) {}    finally {        if (is != null) {            try {                is.close();            } catch (IOException e) {}        }    }    return bitmap;}

測試結果:

最後,我的會話介面與系統的即時同步

監聽系統的會話資料表即可:

public static final Uri THREADS_URI = Uri
.parse("content://mms-sms/conversations?simple=true");

getContentResolver().registerContentObserver(THREADS_URI ,true, sysCallLogObserver);

另外:

我唯寫了自己用的表欄位,如果想欄位別的可以自己列印下:

cursor.getColumnNames(); 


資料:

http://www.cnblogs.com/kakafra/archive/2012/10/06/2713327.html

http://johnsonxu.iteye.com/blog/1406782

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.