您的位置:首页 > 移动开发 > Android开发

Android 中短信数据库的简单操作

2014-08-06 10:00 441 查看
http://blog.csdn.net/zhangzh332/article/details/6396985

Android APK操作短信数据时,不能使用SQLHelper直接操作,需要使用协议,协议使用Uri转义

content://sms/inbox         收件箱  

content://sms/sent           已发送  

content://sms/draft          草稿  

content://sms/outbox      发件中 

content://sms/failed         失败  

content://sms/queued     待发送

 

数据库中sms相关的字段如下:    

 

_id                        
primary key     integer                  与words表内的source_id关联

thread_id             
会话id,一个联系人的会话一个id,与threads表内的_id关联      integer

address                
对方号码          text

person                 
联系人id           integer             

date                     
发件日期           integer

protocol              
通信协议,判断是短信还是彩信     integer       0:SMS_RPOTO, 1:MMS_PROTO

read          
           是否阅读           integer       default 0             0:未读, 1:已读  

status                  
状态                  integer       default-1           -1:接收,

                                                                                                  
0:complete,

                                                                                                
64: pending,

                                                                                               
128: failed 

type      
              短信类型          
integer                                   1:inbox

 
                                                                                                  2:sent

                                                                                                   
3:draft56

         
                                                                                          4:outbox

 
                                                                                                  5:failed

 
                                                                                                  6:queued            

body                     
内容 

service_center     
服务中心号码

subject                 
主题 

reply_path_present 

locked

error_code

seen

具体使用方法:

Cursor cursor = mContentResolver.query(Uri.parse("content://sms"), String[] projection, String selection, String[] selectionArgs,String sortOrder);

if(cursor!=null)

 if(cursor.moveToFirst())

   {String address = cursor .getString(draftCursor.getColumnIndexOrThrow("address"));}

 

 

query转义sql语句时将query函数中的参数转义为

select projection[] from sms where selection[] = selectionArgs[] order by sortOrder

 

由于Android2.2 Messaging中存储草稿短信时不会将address存入sms表中,而以thread_id为索引,将草稿短信的address存入canonical_addresses表中而导致仅根据协议无法查询到draft msgs address(这种设计缺陷是因为Android为了使UI更加效率而使draft msgs不同于其他类型的msgs存储方式所导致的),那么根据这样的转义方式我们可以扩展一下这种select语句使他可以查询到sms表以外的东西:

Cursor draftCursor = mResolver.query(Uri.parse("content://sms"), 

                new String[] {"canonical_addresses.address " +

                        "from sms,threads,canonical_addresses " +

                        "where sms.thread_id=threads._id and threads.recipient_ids=canonical_addresses._id and sms._id ='" + 

                        String.valueOf(target_message_id) + "' --"},

                null, null, null);

 

有点耍滑头,是吧,用到了sql语句中注释符号“--”

这样我们就将这个语句转化为了:

select canonical_addresses.address from sms,threads,canonical_addresses where sms.thread_id=threads._id and threads.recipient_ids=canonical_addresses._id and sms._id = 'target_message_id' -- from sms

 

在sql语句解析的时候,--from sms是不予解析的,所以就成功执行了前面的联合查询操作而得到了我们想要的canonical_addresses表中的address数据。

 

 

简单的Android sms database操作就讲这些,权当抛砖引玉,毕竟群众的力量是无限的嘛

 

以后我将详细讲解下Android mmssms.db数据库中关于sms的内容以及部分短信操作的编码技巧
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: