一、 android sms所要的权限
    <uses-permission android:name="android.permission.READ_SMS" />    <uses-permission android:name="android.permission.RECEIVE_SMS" />


二、 sms发送
与短消息发送相关的类为:SmsManager.
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模式为:
        PendingIntent sentIntent = PendingIntent.getBroadcast(this, 0, new Intent("SMS_SENT"), 0);PendingIntent deliveryIntent = PendingIntent.getBroadcast(this, 0, new Intent("SMS_DELIVERED"), 0);

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


三、 sms接收

根据接收时广播的android.provider.Telephony.SMS_RECEIVED的Intent.我们可以扩展一个BroadcastReceiver来接收sms.
传递的Intent包含pdus数据。相关代码如下:
Bundle bundle = intent.getExtras(); Object[] pdus = (Object[]) bundle.get("pdus"); SmsMessage[] msgs = new SmsMessage[pdus.length]; for (int i = 0; i < pdus.length; i++) {msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);}


四、 采用ContentObserver监控短信数据库

上面方法三中并不能对sms进行更新和删除操作,要做这些操作需要用ContentObserver来监控短信数据库的变化来进行相关操作。
1. 短信数据库的ContentUri
public final static String SMS_URI_ALL =  "content://sms/"; //0public final static String SMS_URI_INBOX = "content://sms/inbox";//1public final static String SMS_URI_SEND = "content://sms/sent";//2public final static String SMS_URI_DRAFT = "content://sms/draft";//3public final static String SMS_URI_OUTBOX = "content://sms/outbox";//4public final static String SMS_URI_FAILED = "content://sms/failed";//5public final static String SMS_URI_QUEUED = "content://sms/queued";//6

2. sms主要结构:
_id => 短消息序号 如100thread_id => 对话的序号 如100address => 发件人地址,手机号.如+8613811810000person => 发件人,返回一个数字就是联系人列表里的序号,陌生人为nulldate => 日期  long型。如1256539465022protocol => 协议 0 SMS_RPOTO, 1 MMS_PROTOread => 是否阅读 0未读, 1已读status => 状态 -1接收,0 complete, 64 pending, 128 failedtype => 类型 1是接收到的,2是已发出body => 短消息内容service_center => 短信服务中心号码编号。如+8613800755500


3. 步骤
a. 写一个类继承ContentObserver
            public class SMSDBObserver extends ContentObserver 

重写onChange方法(里面对INBOX, SEND两个URI进行处理)
public void onChange(boolean selfChange)             Uri smsInBox = Uri.parse(SMS_URI_INBOX);     Cursor c = ctx.getContentResolver().query(uriSms, null, selection, selectionArgs, sortOrder);             //对字段进行操作。。。             //ctx.getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);             //ctx.getContentResolver().update(uri, values, where, selectionArgs);             //ctx.getContentResolver().delete(url, where, selectionArgs);             //ctx.getContentResolver().insert(url, values);


b. 在Activity中注册短信监控
 // 监控短信smsObserver = new SMSDBObserver(new Handler(), this, app);getContentResolver().registerContentObserver(Uri.parse(SMSDBObserver.SMS_URI_ALL), true, smsObserver);


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


更多相关文章

  1. Android 短信 彩信 wap push的接收
  2. Android - 文件读写操作 总结
  3. android rom短信模块最后获取的Cursor字段内容
  4. Google Android手机操作系统发展历程[信息图表]
  5. android 中发送短信
  6. android操作通讯录的联系人
  7. lambda表达式进行对象结合操作的实例详解
  8. 关于操作 ASP.NET Web API的实例
  9. 操作 ASP.NET Web API 的实例教程

随机推荐

  1. 安卓常用属性
  2. Android解析XML文件的三种方式
  3. android:windowSoftInputMode属性使用
  4. 调用Android系统设置中的Intent
  5. android 时区转换
  6. Android中fragment A里面点击button跳转
  7. android ndk 调用第三方so
  8. 更新Anadroid SDK Tooks之后,Eclipse提示N
  9. Unbuntu下Android(安卓)studio报Unable t
  10. Android(安卓)WheelView(滑轮组件)使用