当前位置: 代码迷 >> Android >> android(2.0之后版本) 中读取联系人和通话记录
  详细解决方案

android(2.0之后版本) 中读取联系人和通话记录

热度:103   发布时间:2016-05-01 20:06:36.0
android(2.0以后版本) 中读取联系人和通话记录
http:[email protected]/blog/static/109056781201079113213628/
http://wang-peng1.iteye.com/blog/564284
android 中获取联系人

ContentResolver cr = getContentResolver();        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);        while(cursor.moveToNext()){        //get name        int nameFiledColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);        String contact = cursor.getString(nameFiledColumnIndex);                    String[] PHONES_PROJECTION = new String[] { "_id","display_name","data1","data3"};//            String contactId = cursor.getString(cursor.getColumnIndex(PhoneLookup._ID));            Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PHONES_PROJECTION,                                     ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId, null, null);            //name type ..             while(phone.moveToNext()) {            int i = phone.getInt(0);            String str = phone.getString(1);            str = phone.getString(2);            str = phone.getString(3);            }            phone.close();            //addr            Cursor addrCur = cr.query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI ,         new String[]{"_id","data1","data2","data3"}, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId , null, null);            while(addrCur.moveToNext()) {            int i = addrCur.getInt(0);            String str = addrCur.getString(1);            str = addrCur.getString(2);            str = addrCur.getString(3);            }            addrCur.close();                        //email            Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI ,         new String[]{"_id","data1","data2","data3"}, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId , null, null);            while(emailCur.moveToNext()) {            int i = emailCur.getInt(0);            String str = emailCur.getString(1);            str = emailCur.getString(2);            str = emailCur.getString(3);            }            emailCur.close();                    }        cursor.close();


android中获取通话记录
String str = "";        int type;        long callTime;        Date date;        String time= "";        ContentResolver cr = getContentResolver();        final Cursor cursor = cr.query(CallLog.Calls.CONTENT_URI, new String[]{CallLog.Calls.NUMBER,CallLog.Calls.CACHED_NAME,CallLog.Calls.TYPE, CallLog.Calls.DATE}, null, null,CallLog.Calls.DEFAULT_SORT_ORDER);        for (int i = 0; i < cursor.getCount(); i++) {               cursor.moveToPosition(i);            str = cursor.getString(0);            str = cursor.getString(1);            type = cursor.getInt(2);            SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");            date = new Date(Long.parseLong(cursor.getString(3)));            time = sfd.format(date);           }



读取联系人的名字很简单,但是在读取电话号码时,就需要先去的联系人的ID,然后在通过ID去查找电话号码!一个联系人可能存在多个电话号码!

//得到ContentResolver对象   ContentResolver cr = getContentResolver();     //取得电话本中开始一项的光标   Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);   while (cursor.moveToNext())   {       // 取得联系人名字      int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);       String name = cursor.getString(nameFieldColumnIndex);       string += (name);       // 取得联系人ID       String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));       Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,       ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "              + contactId, null, null);         // 取得电话号码(可能存在多个号码)       while (phone.moveToNext())       {           String strPhoneNumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));           string += (":" + strPhoneNumber);       }       string += "\n";       phone.close();   }   cursor.close();  
  相关解决方案