【android】 短信彩信以及会话读取

转自:http://my.oschina.net/zhuzihasablog/blog/140344

一、需要实现一个同系统信息一样的功能

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

[java]  view plain  copy
  1. String selection = "_id = "+id;  
  2. Uri uri = Uri.parse("content://sms");  
  3. Cursor cursor = contentResolver.query(uri, null, selection, nullnull);  
  4. String phone = cursor.getString(cursor.getColumnIndex("address"));  
  5. int type = cursor.getInt(cursor.getColumnIndex("type"));// 2 = sent, etc.  
  6. String date = cursor.getString(cursor.getColumnIndex("date"));  
  7. String body = cursor.getString(cursor.getColumnIndex("body"));  

2)获取mms

彩信表:content://mms

彩信附件:content://mms/part/

get text content from MMS:

[java]  view plain  copy
  1. String selectionPart = "mid=" + mmsId;  
  2. Uri uri = Uri.parse("content://mms/part");  
  3. Cursor cursor = getContentResolver().query(uri, null,  
  4.     selectionPart, nullnull);  
  5. if (cursor.moveToFirst()) {  
  6.     do {  
  7.         String partId = cursor.getString(cursor.getColumnIndex("_id"));  
  8.         String type = cursor.getString(cursor.getColumnIndex("ct"));  
  9.         if ("text/plain".equals(type)) {  
  10.             String data = cursor.getString(cPart.getColumnIndex("_data"));  
  11.             String body;  
  12.             if (data != null) {  
  13.                 // implementation of this method below  
  14.                 body = getMmsText(partId);  
  15.             } else {  
  16.                 body = cursor.getString(cursor.getColumnIndex("text"));  
  17.             }  
  18.         }  
  19.     } while (cursor.moveToNext());  
  20. }  


[java]  view plain  copy
  1. private String getMmsText(String id) {  
  2.     Uri partURI = Uri.parse("content://mms/part/" + id);  
  3.     InputStream is = null;  
  4.     StringBuilder sb = new StringBuilder();  
  5.     try {  
  6.         is = getContentResolver().openInputStream(partURI);  
  7.         if (is != null) {  
  8.             InputStreamReader isr = new InputStreamReader(is, "UTF-8");  
  9.             BufferedReader reader = new BufferedReader(isr);  
  10.             String temp = reader.readLine();  
  11.             while (temp != null) {  
  12.                 sb.append(temp);  
  13.                 temp = reader.readLine();  
  14.             }  
  15.         }  
  16.     } catch (IOException e) {}  
  17.     finally {  
  18.         if (is != null) {  
  19.             try {  
  20.                 is.close();  
  21.             } catch (IOException e) {}  
  22.         }  
  23.     }  
  24.     return sb.toString();  
  25. }  

获取彩信中图片:

[java]  view plain  copy
  1. String selectionPart = "mid=" + mmsId;  
  2. Uri uri = Uri.parse("content://mms/part");  
  3. Cursor cPart = getContentResolver().query(uri, null,  
  4.     selectionPart, nullnull);  
  5. if (cPart.moveToFirst()) {  
  6.     do {  
  7.         String partId = cPart.getString(cPart.getColumnIndex("_id"));  
  8.         String type = cPart.getString(cPart.getColumnIndex("ct"));  
  9.         if ("image/jpeg".equals(type) || "image/bmp".equals(type) ||  
  10.                 "image/gif".equals(type) || "image/jpg".equals(type) ||  
  11.                 "image/png".equals(type)) {  
  12.             Bitmap bitmap = getMmsImage(partId);  
  13.         }  
  14.     } while (cPart.moveToNext());  
  15. }  

[java]  view plain  copy
  1. private Bitmap getMmsImage(String _id) {  
  2.     Uri partURI = Uri.parse("content://mms/part/" + _id);  
  3.     InputStream is = null;  
  4.     Bitmap bitmap = null;  
  5.     try {  
  6.         is = getContentResolver().openInputStream(partURI);  
  7.         bitmap = BitmapFactory.decodeStream(is);  
  8.     } catch (IOException e) {}  
  9.     finally {  
  10.         if (is != null) {  
  11.             try {  
  12.                 is.close();  
  13.             } catch (IOException e) {}  
  14.         }  
  15.     }  
  16.     return bitmap;  
  17. }  

测试结果:


最后,我的会话界面与系统的实时同步

监听系统的会话数据表即可:

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值