一、 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);
- Intent 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/"; //0
- public final static String SMS_URI_INBOX = "content://sms/inbox";//1
- public final static String SMS_URI_SEND = "content://sms/sent";//2
- public final static String SMS_URI_DRAFT = "content://sms/draft";//3
- public final static String SMS_URI_OUTBOX = "content://sms/outbox";//4
- public final static String SMS_URI_FAILED = "content://sms/failed";//5
- public final static String SMS_URI_QUEUED = "content://sms/queued";//6
2. sms主要結構:
- _id => 短消息序號 如100
- thread_id => 對話的序號 如100
- address => 發件人地址,手機號.如+8613811810000
- person => 發件人,返回一個數字就是聯系人列表里的序號,陌生人為null
- date => 日期 long型。如1256539465022
- protocol => 協議 0 SMS_RPOTO, 1 MMS_PROTO
- read => 是否閱讀 0未讀, 1已讀
- status => 狀態 -1接收,0 complete, 64 pending, 128 failed
- type => 類型 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);
-
-
-
-
-
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