您的位置:首页 > 移动开发 > Android开发

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

2012-10-11 14:51 701 查看
一、需要实现一个同系统信息一样的功能

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



资料:

/article/5101046.html

http://johnsonxu.iteye.com/blog/1406782
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: