现在的位置: 首页 > 综合 > 正文

android 短信格式

2013年08月30日 ⁄ 综合 ⁄ 共 3720字 ⁄ 字号 评论关闭

一、 android sms所要的权限

Java代码  收藏代码
  1. <uses-permission android:name="android.permission.READ_SMS" />  
  2. <uses-permission android:name="android.permission.RECEIVE_SMS" />  



二、 sms发送 
与短消息发送相关的类为:SmsManager. 

Java代码  收藏代码
  1. SmsManager.sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent);  


参数说明: 
destinationAddress the address to send the message to 
scAddress is the service center address or null to use the current default SMSC 
text the body of the message to send 
sentIntent if not NULL this PendingIntent is broadcast when the message is sucessfully sent, or failed. The result code will be Activity.RESULT_OK
for success, or one of these errors: RESULT_ERROR_GENERIC_FAILURE RESULT_ERROR_RADIO_OFF RESULT_ERROR_NULL_PDU. The per-application based SMS control checks sentIntent. If sentIntent is NULL the caller will be checked against all unknown applications, which
cause smaller number of SMS to be sent in checking period. 

deliveryIntent if not NULL this PendingIntent is broadcast when the message is delivered to the recipient. The raw pdu of the status report is in
the extended data ("pdu"). 

其中两个PendingIntent模式为: 

Java代码  收藏代码
  1. PendingIntent sentIntent = PendingIntent.getBroadcast(this0new Intent("SMS_SENT"), 0);  
  2. Intent deliveryIntent = PendingIntent.getBroadcast(this0new Intent("SMS_DELIVERED"), 0);  


        并注册接收器,根据getResultCode()来判断: 

Java代码  收藏代码
  1. registerReceiver(sendReceiver);  
  2. registerReceiver(deliveredReceiver);  



三、 sms接收
 
根据接收时广播的android.provider.Telephony.SMS_RECEIVED的Intent.我们可以扩展一个BroadcastReceiver来接收sms. 
传递的Intent包含pdus数据。相关代码如下: 

Java代码  收藏代码
  1. Bundle bundle = intent.getExtras();  
  2.      Object[] pdus = (Object[]) bundle.get("pdus");  
  3.      SmsMessage[] msgs = new SmsMessage[pdus.length];  
  4.      for (int i = 0; i < pdus.length; i++) {  
  5.         msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);  
  6. }  



四、 采用ContentObserver监控短信数据库
 
上面方法三中并不能对sms进行更新和删除操作,要做这些操作需要用ContentObserver来监控短信数据库的变化来进行相关操作。 
1. 短信数据库的ContentUri 
  

Java代码  收藏代码
  1. public final static String SMS_URI_ALL =  "content://sms/"; //0  
  2. public final static String SMS_URI_INBOX = "content://sms/inbox";//1  
  3. public final static String SMS_URI_SEND = "content://sms/sent";//2  
  4. public final static String SMS_URI_DRAFT = "content://sms/draft";//3  
  5. public final static String SMS_URI_OUTBOX = "content://sms/outbox";//4  
  6. public final static String SMS_URI_FAILED = "content://sms/failed";//5  
  7. public final static String SMS_URI_QUEUED = "content://sms/queued";//6  


2. sms主要结构: 

Java代码  收藏代码
  1. _id => 短消息序号 如100  
  2. thread_id => 对话的序号 如100  
  3. address => 发件人地址,手机号.如+8613811810000  
  4. person => 发件人,返回一个数字就是联系人列表里的序号,陌生人为null  
  5. date => 日期  long型。如1256539465022  
  6. protocol => 协议 0 SMS_RPOTO, 1 MMS_PROTO  
  7. read => 是否阅读 0未读, 1已读  
  8. status => 状态 -1接收,0 complete, 64 pending, 128 failed  
  9. type => 类型 1是接收到的,2是已发出  
  10. body => 短消息内容  
  11. service_center => 短信服务中心号码编号。如+8613800755500  



3. 步骤 
a. 写一个类继承ContentObserver 

Java代码  收藏代码
  1. public class SMSDBObserver extends ContentObserver   


            重写onChange方法(里面对INBOX, SEND两个URI进行处理) 
           

Java代码  收藏代码
  1. public void onChange(boolean selfChange)  
  2.              Uri smsInBox = Uri.parse(SMS_URI_INBOX);  
  3.              Cursor c = ctx.getContentResolver().query(uriSms, null, selection, selectionArgs, sortOrder);  
  4.              //对字段进行操作。。。  
  5.              //ctx.getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);  
  6.              //ctx.getContentResolver().update(uri, values, where, selectionArgs);  
  7.              //ctx.getContentResolver().delete(url, where, selectionArgs);  
  8.              //ctx.getContentResolver().insert(url, values);  



b. 在Activity中注册短信监控 
      

Java代码  收藏代码
  1.  // 监控短信  
  2.         smsObserver = new SMSDBObserver(new Handler(), this, app);  
  3. getContentResolver().registerContentObserver(Uri.parse(SMSDBObserver.SMS_URI_ALL), true, smsObserver);  



注: 
想监控已发送的,就要监控content://sms/send. 
想删除时contentUri只能是content://sms/或content://sms/ conversations
 

抱歉!评论已关闭.