Android sms 发送、接收及格式
2010-03-09 15:44:00 来源:WEB开发网并注册接收器,根据getResultCode()来判断:
Java代码
registerReceiver(sendReceiver);
registerReceiver(deliveredReceiver);
registerReceiver(sendReceiver);
registerReceiver(deliveredReceiver);
三、 sms接收
根据接收时广播的android.provider.Telephony.SMS_RECEIVED的Intent.我们可以扩展一个BroadcastReceiver来接收sms.
传递的Intent包含pdus数据。相关代码如下:
Java代码
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]);
}
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
Java代码
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
public final static String SMS_URI_ALL = "content://sms/"; //0
public final static String SMS_URI_INBOX = "content://sms/inbox";//1
更多精彩
赞助商链接