酷派8295怎样把sms变成mms

android读取短信彩信
会话列表的读取通过查询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 = mCursor.getString(ID);
mSnippetCS = mCursor.getLong(SNIPPET_CS);
if(mSnippetCS ==0){
mSnippet = mCursor.getString(SNIPPET);
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&}; &
final String[] PROJECTION = {&transport_type&,
&thread_id&,
&address&,
&date_sent&,
&error_code&,
&date_sent&,
&msg_box&,
&err_type&,
&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 mD &
& & & & & & public String mThreadId; &
& & & & & & public String mId; &
& & & & & & public boolean mIsS &
& & & & & & public boolean mIsI &
& & & & } &
public class MessageBase {
public long mD
public String mThreadId;
public String mId;
public boolean mIsS
public boolean mIsI
以下是MessageSms的定义:
public class MessageSms extends MessageBase { &
& & & & & & public int mT &
& & & & & & public String mB &
& & & & } &
public class MessageSms extends MessageBase {
public int mT
public String mB
以下是MessageMms的定义:
public class MessageSms extends MessageBase { &
& & & & & & public int mT &
& & & & & & public String mB &
& & & & } &
public class MessageSms extends MessageBase {
public int mT
public String mB
短信中判断某条短信是否为接收的信息:
messageSms.mType = cursor.getInt(cursor.getColumnIndex(&type&)); &
& & & & if(messageSms.mType == 1){ &
& & & & & & messageSms.mIsInbox = &
& & & & } &
& & & & else{ &
& & & & & & messageSms.mIsInbox = &
& & & & } &
messageSms.mType = cursor.getInt(cursor.getColumnIndex(&type&));
if(messageSms.mType == 1){
messageSms.mIsInbox =
messageSms.mIsInbox =
彩信中判断某条彩信是否为接受的信息:
messageMms.mType = cursor.getInt(cursor.getColumnIndex(&msg_box&)); &
& & & & if(messageSms.mType == 1){ &
& & & & & & messageMms.mIsInbox = &
& & & & } &
& & & & else{ &
& & & & & & messageMms.mIsInbox = &
& & & & } &
messageMms.mType = cursor.getInt(cursor.getColumnIndex(&msg_box&));
if(messageSms.mType == 1){
messageMms.mIsInbox =
messageMms.mIsInbox =
读取彩信内容和彩信图片的方法和/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 &
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 &
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表:
&date&, &&
&message_count&, & & & & & & &
&recipient_ids&, &&
&snippet&, &&
&snippet_cs&, &&
&read&, &&
&type&, & & & & & & &&
&error&, &&
&has_attachment& &
&message_count&,
&recipient_ids&,&
&snippet&,&
&snippet_cs&,&
&has_attachment&
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 &
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
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 = s_type &
02-20 16:32:03.888: I/xiao(13974): i = 18 name = svc_type_content &
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
最后的效果如下:
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'--旗下,中国最好的系统开发学习平台!
热门推荐:
当前位置: >
Android接收短信和发送短信
发布时间: 14:54 | 作者:采集侠 | 来源:网络整理 | 浏览:
1.android发送短信
android API 中提供了smsManager类处理短信。其中的sendTextMessage(num, null, content, pend, null)函数就是发送
&&&&&&& 短信的方法。第一个参数为目标者手机号、第二个参数为短信中心地址 null为默认地址、
&&&&&&& 第三个参数短信的文本内容、第四个参数是一个intent会把发送结果带回。第五个参数不知,一般为null。
&&&&&&&&&&&&&&
&&&&&&&&&&&&&&& 一个应用程序要具备发送短信功能,需要在androidManifest.xml中加入android.permission.SEND_SMS权限。
&&&&&&&&&&&&&&
&&&&&&&&&&&&&&& 在模拟器中发送中文会接收方出现乱码的问题,但是在真机中,就不会出现乱码的情况了。所以
&&&&&&& 开发者只需要正常开发短信功能,不需要编码转换。
接收短信也是比较方便的,主要是继承BroadcaseReceiver 类 ,覆盖onReceive 函数:
1:相关类:
android.content.BroadcastReceiver
android.telephony.gsm.SmsM
2:example code.
public class MessageDemo extends BroadcastReceiver {
&&& private static final String strACT = &android.provider.Telephony.SMS_RECEIVED&;
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(strACT)) {
StringBuilder sb = new StringBuilder();
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get(&pdus&);
SmsMessage[] msg = new SmsMessage[pdus.length];
for (int i = 0; i & pdus. i++) {
msg[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
for (SmsMessage currMsg : msg) {
sb.append(&From:&);
sb.append(currMsg.getDisplayOriginatingAddress());
sb.append(&\nMessage:&);
sb.append(currMsg.getDisplayMessageBody());
3: 相关的配置
修改AndroidManifest.xml,在Activity下添加receiver节点:
&receiver android:name=&MessageDemo&&
&intent-filter&
&action android:name=&android.provider.Telephony.SMS_RECEIVED& /&
&/intent-filter&
&/receiver&
随后在application下添加节点:
&uses-permission android:name=&android.permission.SEND_SMS&&&/uses-permission&
&uses-permission android:name=&android.permission.RECEIVE_SMS&&&/uses-permission&
4:使用BroadReceiver的弊端
查看BroadReceiver sdk reference , 可以了解到所有的BroadReceiver对短信的接收是无顺序的状态 ,即使是使用了Ordered broadcasts对于同等优先级别的BroadReceiver ,也会产生无顺序的行为。
所以下面介绍另一种接收短信的行为,包括其中可以进行短信的删除。
5:从数据库端监听sms的收发
//如下 主要用于内部数据库改变,向外面的界面(Activity)做反应
class SMSHandler extends Handler
&&& public void handleMessage(Message msg)
&&&&&&& //Handle message
// 对收到短消息后,做出的处理,这里直接删除,并没有反应到界面,所以上面的handleMessage是空的。
class SMSObserver extends ContentObserver
&&& private Handler m_handle =
&&& public SMSObserver(Handler handle)
&&&&&&& super(handle);
&&&&&&& m_handle =
&&& public void onChange(boolean bSelfChange)
&&&&&&& super.onChange(bSelfChange);
&&&&&&& //Send message to Activity
&&&&&&& Message msg = new Message();
&&&&&&& msg.obj = &xxxxxxxxxx&;
&&&&&&& m_handle.sendMessage(msg);
String strUriInbox = &content://sms/inbox&;
Uri uriSms = Uri.parse(strUriInbox);& //If you want to access all SMS, just replace the uri string to &content://sms/&
Cursor c = mContext.getContentResolver().query(uriSms, null, null, null, null);
// delete all sms here when every new sms occures.
while (c.moveToNext())
&&&&&& //Read the contents of the SMS;
&&&&&& for( i & c.getColumnCount(); i++)
&&&&&&&&&&& String strColumnName = c.getColumnName(i);
&&&&&&&&&&& String strColumnValue = c.getString(i);
&&&&&& //Delete the SMS
&&&&&&& String pid = c.getString(1);& //G
&&&&&& String uri = &content://sms/conversations/& +
&&&&&& mContext.getContentResolver().delete(Uri.parse(uri), null, null);&&&&&&
//把基本类功能性地应用起来
ContentResolver contentResolver = getContentResolver();// Context 环境下getContentResolver()
Handler handler = new SMSHandler();
ContentObserver m_SMSObserver = new SMSObserver(handler);
contentResolver.registerContentObserver(Uri.parse(&content://sms/inbox&),true, m_SMSObserver);
//Register to observe SMS in outbox,we can observe SMS in other location by changing Uri string, such as inbox, sent, draft, outbox, etc.)
// some Available Uri string& for sms.
& String strUriInbox = &content://sms/inbox&;//SMS_INBOX:1
& String strUriFailed = &content://sms/failed&;//SMS_FAILED:2
& String strUriQueued = &content://sms/queued&;//SMS_QUEUED:3
& String strUriSent = &content://sms/sent&;//SMS_SENT:4
& String strUriDraft = &content://sms/draft&;//SMS_DRAFT:5
& String strUriOutbox = &content://sms/outbox&;//SMS_OUTBOX:6
& String strUriUndelivered = &content://sms/undelivered&;//SMS_UNDELIVERED
& String strUriAll = &content://sms/all&;//SMS_ALL
& String strUriConversations = &content://sms/conversations&;//you can delete one conversation by thread_id
& String strUriAll = &content://sms&//you can delete one message by _id
REMEBER: must request following permission
1) Read SMS
&&& &uses-permssion android:name=&android.permission.READ_SMS& /&
2) Delete/Modify/Send SMS
&&& &uses-permssion android:name=&android.permission.WRITE_SMS& /&
in AndroidManifest.xml
Android数据库总结:

我要回帖

更多关于 sms mms 驱动 问号 的文章

 

随机推荐