android短彩信查询以及MMS表结构

android短信的数据库的Uri是不公开的, 读取起来时灰常不方便的, 这里做了下总结.
用adb指令将mmssms.db从/data/data/com.android.providers.telephony/databases中pull出来
经常使用到的表有
canonical_addresses, sms, threads三个表格
sms是存储着所有的短信, 主要的列有_id, thread_id, address, person, date, read, type, body
关于的sms的Uri有
发件箱 content://sms/outbox
收件箱 content://sms/inbox
草稿箱 content://sms/draft
conversations content://sms/conversations
threads表存储着每一个短信对话的线程. 主要列有_id, date, message_count, recipient_ids, snippet, read
recipient_ids 存放的是参与此次对话的person的id, 然而这个id不是通讯录里面的id, 而是canonical_addresses 的id. 这就是canonical_addresses 表格的作用
threads 表 uri: content://mms-sms/conversations?simple=true
canonical_addresses 表 uri content://mms-sms/canonical-addresses

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

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 plaincopyprint?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"));

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:
[java] view plaincopyprint?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());
}

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


[java] view plaincopyprint?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();
}

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

获取彩信中图片:


[java] view plaincopyprint?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());
}

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());
}
[java] view plaincopyprint?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;
}

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();
========================================
数据库中sms相关的字段如下:



_id primary key integer 与words表内的source_id关联

thread_id 会话id,一个联系人的会话一个id,与threads表内的_id关联 integer

address 对方号码 text

person 联系人id integer

date 发件日期 integer

protocol 通信协议,判断是短信还是彩信 integer 0:SMS_RPOTO, 1:MMS_PROTO

read 是否阅读 integer default 0 0:未读, 1:已读

status 状态 integer default-1 -1:接收,

0:complete,

64: pending,

128: failed

type 短信类型 integer 1:inbox

2:sent

3:draft56

4:outbox

5:failed

6:queued

body 内容

service_center 服务中心号码

subject 主题

reply_path_present

locked

error_code

seen
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值