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_idsaddress对应sms表里面的address

Android 读取手机通讯录内容_第1张图片

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

Android 读取手机通讯录内容_第2张图片

_id                      primary key     integerwords表内的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 读取手机通讯录内容_第3张图片

Threads 表结构

Android 读取手机通讯录内容_第4张图片

接下来,需要了解一些基本的知识

ContentResolver

待续………

下面给出一些事例

从threads表获取日期,消息数量,部分消息内容,从sms表里面取得threads里_id对应的电话号码

 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();   

引用
Sms table :http://blog.csdn.net/u013037007/article/details/37696401
totalTable :http://blog.csdn.net/u010335298/article/details/42778213

你可能感兴趣的:(android,基础知识)