Android SMS相关操作

1  Android的SMS短消息格式和主要字段

  android的sms结构和主要字段如下:

其他代码 复制代码
  1. _id                 // 短消息序号   
  2. thread_id           // 对话的序号(conversation)   
  3. address             // 收件人   
  4. person              // 发件人   
  5. date                // 日期   
  6. protocol            // 协议   
  7. read                // 是否阅读   
  8. status              // 状态   
  9. type                // 类型   
  10. reply_path_present  //    
  11. subject             // 主题   
  12. body                // 短消息内容   
  13. service_center      // 服务中心  

 2 获取机器中的短消息

见代码,比较简单:

 

Java代码 复制代码
  1. sms = new ArrayList<Map<String, Object>>();   
  2. Cursor c = getContentResolver().query(uriSms, nullnullnull,   
  3.         null);   
  4. while (c.moveToNext()) {   
  5.     try {   
  6.         item = new HashMap<String, Object>();   
  7.   
  8.         // Read the contents of the SMS;   
  9.         for (int i = 0; i < c.getColumnCount(); i++) {   
  10.             String strColumnName = c.getColumnName(i);   
  11.             String strColumnValue = c.getString(i);   
  12.   
  13.             item.put(strColumnName, strColumnValue);   
  14.         }   
  15.     } catch (Exception e) {   
  16.         Log.w("Exception:", e.getMessage());   
  17.     }   
  18. sms.add(item);  
sms = new ArrayList<Map<String, Object>>();
Cursor c = getContentResolver().query(uriSms, null, null, null,
		null);
while (c.moveToNext()) {
	try {
		item = new HashMap<String, Object>();

		// Read the contents of the SMS;
		for (int i = 0; i < c.getColumnCount(); i++) {
			String strColumnName = c.getColumnName(i);
			String strColumnValue = c.getString(i);

			item.put(strColumnName, strColumnValue);
		}
	} catch (Exception e) {
		Log.w("Exception:", e.getMessage());
	}
sms.add(item);

 3 总结

3.1 短消息

    android中短消息字段比较多,但不是每个字段都是必填,根据自己实际开发需要。

3.2 发送和接收

3.2.1 发送短消息
    发送短消息比较简单,API直接就有send方法。需要注意的是:短消息长度的控制,发送状态的获取。
3.2.2 接收短消息
    主要思想是注册成为服务,并进行监听接收到新短消息时的系统通知,然后进行后续操作。网上代码很多,不多论述。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值