`

Android SMS 数据库

阅读更多
$ adb shell
$ cd data/data
$ cd com.android.providers.telephony
$ cd databases
$ sqlite3 mmssms.db
sqlite> .tables
addr                 pdu                  threads           
android_metadata     pending_msgs         words             
attachments          rate                 words_content     
canonical_addresses  raw                  words_segdir      
drm                  sms                  words_segments    
part                 sr_pending  

会发现mmssms一共17个表

数据库中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语句中注释符号“--”
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics