天天看點

Android 讀取手機通訊錄内容

滿足條件

1.手機root ,或者使用模拟器

2.使用monitor打開 FileExplorer 依次點選如入路徑(短信資料庫路徑)

/data/data/com.Android.providers.telephony

3.Sqlite 資料庫 檢視軟體

4.導出資料庫到桌面,或者其他地方,使用sqlite進行檢視

這裡我們隻關注 三張表

canonical_addresses,threads,Smss

canonical_addresses表結構代表的含義

id對應threads表裡面的recipient_ids,address對應sms表裡面的address

Android 讀取手機通訊錄内容

資料庫中sms 表 相關的字段如下:

Android 讀取手機通訊錄内容
_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, 128failed
type         短信類型           integer 1:inbox  2:sent 3:draft 56  4:outbox  5:failed  6:queued
body                      内容 
service_center      服務中心号碼 
subject                  主題  reply_path_present  
locked 
error_code 
seen      

如果監聽sms資料庫變化,發送一條短信要經過type的6,4,2三個狀态變化,如果隻想監聽接受到的短信内容

判斷type=1即可,如果判斷發送短信,判斷type=2即可,這樣就不會出現重複操作。

Threads表————這個相當于手機接收短信那個界面,是按聯系人分的會話清單

Android 讀取手機通訊錄内容

Threads 表結構

Android 讀取手機通訊錄内容

接下來,需要了解一些基本的知識

ContentResolver

下面給出一些事例

ContentResolver resolver = getContentResolver();  
            Cursor cursor = resolver.query(Uri.parse("content://mms-sms/conversations"),   
                    new String[]{ "* from threads--" },   
                    null,   
                    null,   
                    null);  
            System.out.println(cursor.getCount());  
            if(cursor.getCount()>0){  
                while (cursor.moveToNext()) {  
                    MyMessageList messageList = new MyMessageList();  
                    messageList.setThreadid((cursor.getString(cursor.getColumnIndex("_id"))));  
                    SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd hh:mm");  
                    Date date = new Date(Long.parseLong(cursor.getString(cursor.getColumnIndex("date"))));  
                    String time = sfd.format(date);  
                    messageList.setDate(time);  
                    messageList.setMessagecount(cursor.getString(cursor.getColumnIndex("message_count")));  
                    messageList.setSnippet(cursor.getString(cursor.getColumnIndex("snippet")));               

                    ContentResolver resolver2 = getContentResolver();  
                    Cursor cursor2 = resolver2.query(Uri.parse("content://sms/"),   
                            new String[]{"address"},   
                            "thread_id=?",   
                            new String[]{cursor.getString((cursor.getColumnIndex("_id")))},  
                            null);  
                        if(cursor2.moveToNext()){  
                            System.out.println(cursor2.getString(cursor2.getColumnIndex("address")));  
                            messageList.setPhone(cursor2.getString(cursor2.getColumnIndex("address")));                   
                        }                     
                    cursor2.close();  
                    myMessageLists.add(messageList);  
                }  
                System.out.println(myMessageLists.size());  
            }  

            cursor.close();      

下面是從sms中擷取具體的和某個人通話的短信

ContentResolver resolver = getContentResolver();  
            Cursor cursor = resolver.query(Uri.parse("content://sms/"),   
                    new String[]{"date","body","type"},   
                    "thread_id=?",   
                    new String[]{threadidString},  
                    "date desc");                 
            if(cursor.getCount()>0){  
                cursor.moveToFirst();  
                System.out.println(cursor.getPosition());  
                DetailMessage detailMessage = new DetailMessage();  
                detailMessage.setMessageString(cursor.getString(cursor.getColumnIndex("body")));  
                SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd hh:mm");  
                Date date = new Date(Long.parseLong(cursor.getString(cursor.getColumnIndex("date"))));  
                String time = sfd.format(date);  
                System.out.println(time);  
//              System.out.println(cursor.getString(cursor.getColumnIndex("date")));  
                detailMessage.setDateString(time);  
                detailMessage.setType(cursor.getInt(cursor.getColumnIndex("type")));  
                arrayList.add(detailMessage);  
                while (cursor.moveToNext()) {  
                    System.out.println(cursor.getPosition());  
                    detailMessage = new DetailMessage();  
                    detailMessage.setMessageString(cursor.getString(cursor.getColumnIndex("body")));  
                    date = new Date(Long.parseLong(cursor.getString(cursor.getColumnIndex("date"))));  
                    time = sfd.format(date);  
                    detailMessage.setDateString(time);  
                    detailMessage.setType(cursor.getInt(cursor.getColumnIndex("type")));  
                    arrayList.add(detailMessage);                 
                }  
            }                         
            cursor.close();