你好,游客 登录 注册 搜索
背景:
阅读新闻

Android SMS 短信操作

[日期:2012-07-30] 来源:Linux社区  作者:berdy [字体:大 中 小]

sms表中的主要字段介绍:
_id             短消息序号   
thread_id       关联threads表中的id,对话的序号   
address         发件人地址,手机号
person          发件人,返回一个数字就是联系人列表里的序号
date            日期  long型  
protocol        协议 0 SMS_RPOTO, 1 MMS_PROTO   
read            是否阅读 0未读, 1已读   
status          状态 -1接收,0 complete, 64 pending, 128 failed   
type            类型 1是接收到的,2是已发出   
body            短消息内容   
service_center  短信服务中心号码编号

  1. // 读取收件箱中指定号码的短信   
  2. Cursor cursor = managedQuery(Uri.parse("content://sms/inbox"),  
  3.         new String[] { "_id", "address", "body", "read" }, " address=? and read=?", new String[] { "106597281",  
  4.                 "0" }, "date desc");  
  5. if (cursor != null) {  
  6.     ContentValues values = new ContentValues();  
  7.     values.put("read", "1");  
  8.     // 修改短信为已读模式   
  9.     cursor.moveToFirst();  
  10.     if (cursor.moveToFirst()) {  
  11.         // 根据id修改短信的状态为已读状态   
  12.         getContentResolver().update(Uri.parse("content://sms/inbox"), values, " _id=?",  
  13.                 new String[] { "" + cursor.getInt(0) });  
  14.         // 删除指定号码的短信   
  15.         int smsId = cursor.getInt(0);  
  16.         String msgbody = cursor.getString(cursor.getColumnIndexOrThrow("body"));  
  17.         try {  
  18.             msgbody = (new String(msgbody.getBytes(), "utf8")).trim();  
  19.         } catch (UnsupportedEncodingException e) {  
  20.             Log.e("TAG", "", e);  
  21.         }  
  22.         // 删除指定短信   
  23.         getContentResolver().delete(Uri.parse("content://sms/" + smsId), null, null);  
  24.     }  
  25. }  
  26. cursor.close();  

另外值得注意的是调用SMSManager.sendTextMessage接口发送短信是不会将信息写入到sms表中的,若需要记录到表中就需要添加写入sms表的操作了。

总的来说对短信的操作还是挺简单的,复杂点的就是需要考虑提升从表中读取数据的效率。

linux
相关资讯       Android SMS 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款