现在的位置: 首页 > 综合 > 正文

android 通话记录和联系人查询

2013年02月25日 ⁄ 综合 ⁄ 共 3901字 ⁄ 字号 评论关闭

呼叫记录有三种类型:

来电:CallLog.Calls.INCOMING_TYPE (常量值:1)

已拨:CallLog.Calls.OUTGOING_TYPE(常量值:2)

未接:CallLog.Calls.MISSED_TYPE(常量值:3)

查看源码中的声明:

<provider android:name="CallLogProvider"
android:authorities="call_log"
android:syncable="false"
android:multiprocess="false"
android:readPermission="android.permission.READ_CONTACTS"
android:writePermission="android.permission.WRITE_CONTACTS">

</provider>

需要声明的权限

<uses-permission android:name="android.permission.READ_CONTACTS" />

<uses-permission android:name="android.permission.WRITE_CONTACTS" />

系统的通话记录,是通过 ContentProvider 来对外共享的 Uri CallLog.Calls.CONTENT_URI :

等价于:Uri.parse("content://call_log/calls");

查询出所有记录

ContentResolver resolver = getContentResolver(); resolver.query(CallLog.Calls.CONTENT_URI, null, null, new String[]{"15101689022"}, null);

查询某一个联系人的所有记录(按电话号码)

resolver.query(CallLog.Calls.CONTENT_URI, null, "number=?", new String[]{"15101689022"}, null);

查询某一个联系人的所有未接电话记录(按电话号码) resolver.query(CallLog.Calls.CONTENT_URI, String[]{"15101689022"}, null);

删除某联系人最近的一次来电 ContentResolver resolver = getContentResolver();

/* 这里涉及到内容提供者的知识,其实这里是直接在操作
Android
的数据库,十分痛苦 */

Cursor cursor = resolver.query(CallLog.Calls.CONTENT_URI, new String[]{"_id"}, "number=? and (type=1 or type=3)", new String[]{"15101689022"}, "_id desc limit 1"); null, "number=? and type=3", new if(cursor.moveToFirst()) { int id = cursor.getInt(0); resolver.delete(CallLog.Calls.CONTENT_URI,
"_id=?", new String[] {id + ""}); }

下面说说代码是怎么用的

先说说 Phone.CONTENT_URI,获取联系人的时候需要去这个url中去找数据 。它所指向的其实是“content:// com.android.contacts/data/phones”。这个url 对应着contacts表 和 raw_contacts表 以及 data表 所以说我们的数据都是从这三个表中获取的。

这里强调一下query第二个参数

private static final String[] PHONES_PROJECTION = new String[] {

    Phone.DISPLAY_NAME, Phone.NUMBER, Photo.PHOTO_ID,Phone.CONTACT_ID };

它的意思是只去表中找 显示名称 电话号码 头像ID 联系人ID 这4个数据 ,如果你须要其它数据 的话 就须要修改这里。

获得手机通讯录联系人信息

/**得到手机通讯录联系人信息**/  

   private void getPhoneContacts() {  

ContentResolver resolver = mContext.getContentResolver();  

// 获取手机联系人  

Cursor phoneCursor = resolver.query(Phone.CONTENT_URI,PHONES_PROJECTION, null, null, null);  

if (phoneCursor != null) {  

    while (phoneCursor.moveToNext()) {  

    //得到手机号码  

    String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX);  

    //当手机号码为空的或者为空字段 跳过当前循环  

    if (TextUtils.isEmpty(phoneNumber))  

        continue;  

    //得到联系人名称  

    String contactName = phoneCursor.getString(PHONES_DISPLAY_NAME_INDEX);  

    //得到联系人ID  

    Long contactid = phoneCursor.getLong(PHONES_CONTACT_ID_INDEX);  

    //得到联系人头像ID  

    Long photoid = phoneCursor.getLong(PHONES_PHOTO_ID_INDEX);  

    //得到联系人头像Bitamp  

    Bitmap contactPhoto = null;  

    //photoid 大于0 表示联系人有头像 如果没有给此人设置头像则给他一个默认的  

    if(photoid > 0 ) {  

        Uri uri =ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,contactid);  

        InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(resolver, uri);  

        contactPhoto = BitmapFactory.decodeStream(input);  

    }else {  

        contactPhoto = BitmapFactory.decodeResource(getResources(), R.drawable.contact_photo);  

    }  

    mContactsName.add(contactName);  

    mContactsNumber.add(phoneNumber);  

    mContactsPhonto.add(contactPhoto);  

    }  

    phoneCursor.close();  

}  

   }  

获得手机sim卡联系人信息

sim卡和手机本人 获取的方式类似 只是url有点不一样 ,须要注意的一点是 sim卡 是没有联系人头像的。

 /**得到手机SIM卡联系人人信息**/  

   private void getSIMContacts() {  

ContentResolver resolver = mContext.getContentResolver();  

// 获取Sims卡联系人  

Uri uri = Uri.parse("content://icc/adn");  

Cursor phoneCursor = resolver.query(uri, PHONES_PROJECTION, null, null,  

    null);  

if (phoneCursor != null) {  

    while (phoneCursor.moveToNext()) {  

  

    // 得到手机号码  

    String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX);  

    // 当手机号码为空的或者为空字段 跳过当前循环  

    if (TextUtils.isEmpty(phoneNumber))  

        continue;  

    // 得到联系人名称  

    String contactName = phoneCursor  

        .getString(PHONES_DISPLAY_NAME_INDEX);  

    //Sim卡中没有联系人头像 

    mContactsName.add(contactName);  

    mContactsNumber.add(phoneNumber);  

    }  

    phoneCursor.close();  

}  

   }

抱歉!评论已关闭.