由于项目需要读取手机内的短信和彩信数据,不知道如何来处理,本来这个项目是别人做的,后来让我来修改,都是百度点代码粘贴上去,一问为啥这样处理,都是说网上找的代码,我对我修改后的东西做了下整理,首先感谢http://kaifazhe001.com/article/actueal_-8060143.html这篇博客,给了我很大的帮助,里面有些东西是看系统源码来处理的。
会话列表的读取通过查询content://mms-sms/conversations?simple=true这个Url可以获得到,由于里面有彩信和短信,处理方式如下:
mThreadId = mCursor.getString(ID);
mSnippetCS = mCursor.getLong(SNIPPET_CS);
if(mSnippetCS ==0){
mSnippet = mCursor.getString(SNIPPET);
}
else{
try {
mSnippet = new String(
mCursor.getString(SNIPPET).getBytes("ISO8859_1"), "utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
其中mThreadId就是短信及彩信数据库中所需要的threadid。
点击某个会话列表进入和某个联系人所有的聊天记录时,系统的实现方式是通过查询url:content://mms-sms/conversations/+会话Id(mThreadId),查询的列项不能为空,我试着写成NULL去查,每次都会出现空指针异常,源码内这个参数是:
final String[] PROJECTION = {"transport_type",
"_id",
"thread_id",
"address",
"body",
"date",
"date_sent",
"read",
"type",
"status",
"locked",
"error_code",
"sub",
"sub_cs",
"date",
"date_sent",
"read",
"m_type",
"msg_box",
"d_rpt",
"rr",
"err_type",
"locked",
"sim_id",
"sim_id",
"service_center",
"service_center"};
查询完得到Cursor后,短信内容可以直接提取出来,这个短信和彩信的排列是按照日期已经排列好了,但彩信的内容系统源码是通过一个pud包来处理的,这个源码我复制到本地后会用到系统内的.class文件中的方法,所以只能终止这样处理,我想是既然这个彩信和短信已经排列好了,而且短信的内容已经可以获取到了,那我直接从彩信数据库中读取彩信就可以了(content://mms),只要查询出彩信数据库中的id和当前需要的彩信的id一样的彩信就可以了,但这个地方没有办法获取到id(反正我是没有找到,如果你知道的话,麻烦告诉我一下),所以又终止了。
所以我是先从短信数据库中查询出threadid和现在的threadid一样的所有短信(content://sms),然后把date,body,是否为发送的短信存储在一个对象中(我自己定义的类对象),然后把这个对象存储在List<MessageBase> mMessageList中,然后从彩信数据库中查询出threadid和现在的threadid一样的所有彩信(content://mms),然后把date, subject, 图片,是否为发送的彩信存储在一个对象中(我自己定义的类对象),然后存储在mMessageList中,当然彩信和短信对象的类都是继承自一个基类,然后对mMessageList依据date相进行排序,在这个排序的过程中由于彩信是以秒来记录的,而短信是以毫秒来记录的,所以在排序前需要进行1000的缩放,排好序后的mMessageList和手机短信中的列表一致了,然后进行显示,这个显示是采用一个ListView,重写Adapter(如上一篇博客--重写ListView Adapter)。
以下是MessageBase的定义:
public class MessageBase {
public long mDate;
public String mThreadId;
public String mId;
public boolean mIsSms;
public boolean mIsInbox;
}
以下是MessageSms的定义:
public class MessageSms extends MessageBase {
public int mType;
public String mBody;
}
以下是MessageMms的定义:
public class MessageSms extends MessageBase {
public int mType;
public String mBody;
}
短信中判断某条短信是否为接收的信息:
messageSms.mType = cursor.getInt(cursor.getColumnIndex("type"));
if(messageSms.mType == 1){
messageSms.mIsInbox = true;
}
else{
messageSms.mIsInbox = false;
}
彩信中判断某条彩信是否为接受的信息:
messageMms.mType = cursor.getInt(cursor.getColumnIndex("msg_box"));
if(messageSms.mType == 1){
messageMms.mIsInbox = true;
}
else{
messageMms.mIsInbox = false;
}
读取彩信内容和彩信图片的方法和http://kaifazhe001.com/article/actueal_-8060143.html博客中写的一样,注意其中需要的"mid = ?"这个id是mms中的id,不是threadid。
各个Url表中所拥有的列名如下:
mms/part表:
02-20 17:19:55.224: I/xiao(19370): i = 0 name = _id
02-20 17:19:55.224: I/xiao(19370): i = 1 name = mid
02-20 17:19:55.224: I/xiao(19370): i = 2 name = seq
02-20 17:19:55.224: I/xiao(19370): i = 3 name = ct
02-20 17:19:55.224: I/xiao(19370): i = 4 name = name
02-20 17:19:55.224: I/xiao(19370): i = 5 name = chset
02-20 17:19:55.224: I/xiao(19370): i = 6 name = cd
02-20 17:19:55.224: I/xiao(19370): i = 7 name = fn
02-20 17:19:55.224: I/xiao(19370): i = 8 name = cid
02-20 17:19:55.224: I/xiao(19370): i = 9 name = cl
02-20 17:19:55.224: I/xiao(19370): i = 10 name = ctt_s
02-20 17:19:55.224: I/xiao(19370): i = 11 name = ctt_t
02-20 17:19:55.224: I/xiao(19370): i = 12 name = _data
02-20 17:19:55.224: I/xiao(19370): i = 13 name = text
mms-sms/conversation/threadid表:
01-03 10:51:26.674: I/xiao(3343): i = transport_type
01-03 10:51:26.674: I/xiao(3343): i = _id
01-03 10:51:26.674: I/xiao(3343): i = thread_id
01-03 10:51:26.674: I/xiao(3343): i = address
01-03 10:51:26.674: I/xiao(3343): i = body
01-03 10:51:26.675: I/xiao(3343): i = date
01-03 10:51:26.675: I/xiao(3343): i = date_sent
01-03 10:51:26.675: I/xiao(3343): i = read
01-03 10:51:26.675: I/xiao(3343): i = type
01-03 10:51:26.676: I/xiao(3343): i = status
01-03 10:51:26.676: I/xiao(3343): i = locked
01-03 10:51:26.677: I/xiao(3343): i = error_code
01-03 10:51:26.677: I/xiao(3343): i = sub
01-03 10:51:26.677: I/xiao(3343): i = sub_cs
01-03 10:51:26.678: I/xiao(3343): i = date
01-03 10:51:26.678: I/xiao(3343): i = date_sent
01-03 10:51:26.678: I/xiao(3343): i = read
01-03 10:51:26.678: I/xiao(3343): i = m_type
01-03 10:51:26.678: I/xiao(3343): i = msg_box
01-03 10:51:26.678: I/xiao(3343): i = d_rpt
01-03 10:51:26.678: I/xiao(3343): i = rr
01-03 10:51:26.680: I/xiao(3343): i = err_type
01-03 10:51:26.680: I/xiao(3343): i = locked
01-03 10:51:26.680: I/xiao(3343): i = sim_id
01-03 10:51:26.680: I/xiao(3343): i = sim_id
01-03 10:51:26.680: I/xiao(3343): i = service_center
01-03 10:51:26.680: I/xiao(3343): i = service_center
MmsSms.TYPE_DISCRIMINATOR_COLUMN,
BaseColumns._ID,
Conversations.THREAD_ID,
// For SMS
Sms.ADDRESS,
Sms.BODY,
Sms.DATE,
Sms.DATE_SENT,
Sms.READ,
Sms.TYPE,
Sms.STATUS,
Sms.LOCKED,
Sms.ERROR_CODE,
// For MMS
Mms.SUBJECT,
Mms.SUBJECT_CHARSET,
Mms.DATE,
Mms.DATE_SENT,
Mms.READ,
Mms.MESSAGE_TYPE,
Mms.MESSAGE_BOX,
Mms.DELIVERY_REPORT,
Mms.READ_REPORT,
PendingMessages.ERROR_TYPE,
Mms.LOCKED,
//a0
Sms.SIM_ID,
Mms.SIM_ID,
Sms.SERVICE_CENTER,
Mms.SERVICE_CENTER
mms-sms/conversation表:
"_id",
"date",
"message_count",
"recipient_ids",
"snippet",
"snippet_cs",
"read",
"type",
"error",
"has_attachment"
mms表:
02-20 17:00:28.810: I/xiao(17191): i = 0 name = _id
02-20 17:00:28.810: I/xiao(17191): i = 1 name = thread_id
02-20 17:00:28.810: I/xiao(17191): i = 2 name = date
02-20 17:00:28.810: I/xiao(17191): i = 3 name = msg_box
02-20 17:00:28.810: I/xiao(17191): i = 4 name = read
02-20 17:00:28.810: I/xiao(17191): i = 5 name = m_id
02-20 17:00:28.810: I/xiao(17191): i = 6 name = sub
02-20 17:00:28.810: I/xiao(17191): i = 7 name = sub_cs
02-20 17:00:28.810: I/xiao(17191): i = 8 name = ct_t
02-20 17:00:28.810: I/xiao(17191): i = 9 name = ct_l
02-20 17:00:28.810: I/xiao(17191): i = 10 name = exp
02-20 17:00:28.810: I/xiao(17191): i = 11 name = m_cls
02-20 17:00:28.810: I/xiao(17191): i = 12 name = m_type
02-20 17:00:28.810: I/xiao(17191): i = 13 name = v
02-20 17:00:28.810: I/xiao(17191): i = 14 name = m_size
02-20 17:00:28.810: I/xiao(17191): i = 15 name = pri
02-20 17:00:28.810: I/xiao(17191): i = 16 name = rr
02-20 17:00:28.810: I/xiao(17191): i = 17 name = rpt_a
02-20 17:00:28.817: I/xiao(17191): i = 18 name = resp_st
02-20 17:00:28.817: I/xiao(17191): i = 19 name = st
02-20 17:00:28.817: I/xiao(17191): i = 20 name = tr_id
02-20 17:00:28.817: I/xiao(17191): i = 21 name = retr_st
02-20 17:00:28.817: I/xiao(17191): i = 22 name = retr_txt
02-20 17:00:28.817: I/xiao(17191): i = 23 name = retr_txt_cs
02-20 17:00:28.817: I/xiao(17191): i = 24 name = read_status
02-20 17:00:28.817: I/xiao(17191): i = 25 name = ct_cls
02-20 17:00:28.817: I/xiao(17191): i = 26 name = resp_txt
02-20 17:00:28.817: I/xiao(17191): i = 27 name = d_tm
02-20 17:00:28.817: I/xiao(17191): i = 28 name = d_rpt
02-20 17:00:28.817: I/xiao(17191): i = 29 name = locked
02-20 17:00:28.817: I/xiao(17191): i = 30 name = seen
02-20 17:00:28.817: I/xiao(17191): i = 31 name = deletable
sms表:
202-20 16:32:03.880: I/xiao(13974): i = 0 name = _id
02-20 16:32:03.888: I/xiao(13974): i = 1 name = thread_id
02-20 16:32:03.888: I/xiao(13974): i = 2 name = address
02-20 16:32:03.888: I/xiao(13974): i = 3 name = person
02-20 16:32:03.888: I/xiao(13974): i = 4 name = date
02-20 16:32:03.888: I/xiao(13974): i = 5 name = protocol
02-20 16:32:03.888: I/xiao(13974): i = 6 name = read
02-20 16:32:03.888: I/xiao(13974): i = 7 name = status
02-20 16:32:03.888: I/xiao(13974): i = 8 name = type
02-20 16:32:03.888: I/xiao(13974): i = 9 name = reply_path_present
02-20 16:32:03.888: I/xiao(13974): i = 10 name = subject
02-20 16:32:03.888: I/xiao(13974): i = 11 name = body
02-20 16:32:03.888: I/xiao(13974): i = 12 name = service_center
02-20 16:32:03.888: I/xiao(13974): i = 13 name = locked
02-20 16:32:03.888: I/xiao(13974): i = 14 name = error_code
02-20 16:32:03.888: I/xiao(13974): i = 15 name = seen
02-20 16:32:03.888: I/xiao(13974): i = 16 name = deletable
02-20 16:32:03.888: I/xiao(13974): i = 17 name = svc_type
02-20 16:32:03.888: I/xiao(13974): i = 18 name = svc_type_content
最后的效果如下: