android简单应用(一)

如何查询手机数据库联系人

  在android中通话记录保存在contact2.db中的calls数据表中,我们可以利用ContentProvider查询Calls表中的数据,Calls表的URI:CallLog.Calls.CONTENT_URI;

Calls表中的列所对应的常量:
_id      ----> CallLog.Calls._ID
photo_id ----> CallLog.Calls.CACHED_PHOTO_ID/"photo_id"
type     ----> CallLog.Calls.TYPE
number   ----> CallLog.Calls.NUMBER;
time     ----> CallLog.Calls.DATE;
name     ----> CallLog.Calls.CACHED_NAME;

完整的查询联系人详细信息的代码:

public static List<Contact> getAllContacts(Context context){
        List<Contact> contacts = new ArrayList<Contact>();
        ContentResolver cr = context.getContentResolver();
        //从contacts数据表中查询回来的全部内容
        Cursor cursor = cr.query(Contacts.CONTENT_URI, new String[]{Contacts._ID,Contacts.PHOTO_ID,Contacts.LOOKUP_KEY}, null, null, null);
        while(cursor.moveToNext()){
            Contact contact = new Contact();
            contact.set_id(cursor.getInt(0));
            contact.setPhoto_id(cursor.getInt(1));
            contact.setLookupKey(cursor.getString(2));
            //从Data表中查询这个联系人的姓名、电话、邮件等详细信息
            ContentResolver cr2 = context.getContentResolver();
            Cursor c = cr2.query(Data.CONTENT_URI,
                    new String[]{Data.MIMETYPE,Data.DATA1},
                    Data.RAW_CONTACT_ID + " = ?",
                    new String[]{String.valueOf(contact.get_id())},
                    null);
            while(c.moveToNext()){
                String mimeType = c.getString(0);
                if(mimeType.equals("vnd.android.cursor.item/email_v2")){
                    contact.setEmail(c.getString(1));
                }else if(mimeType.equals("vnd.android.cursor.item/organization")){
                    contact.setCompany(c.getString(1));
                }else if(mimeType.equals("vnd.android.cursor.item/phone_v2")){
                    contact.setNumber(c.getString(1));
                }else if(mimeType.equals("vnd.android.cursor.item/name")){
                    contact.setName(c.getString(1));
                }else if(mimeType.equals("vnd.android.cursor.item/postal-address_v2")){
                    contact.setAddress(c.getString(1));
                }
            }
            c.close();
            contacts.add(contact);
        }
        cursor.close();

        return contacts;
    }


    在这里我们需要知道的是calls表中的photo_id列数据存在一个小问题,当对方拨打电话进来时,如果没有接听,则对方的头像ID值是不会记录在calls表中的。此时CalllogFragment中显示该条通话记录的时候就不会有头像出现。所以,需要利用电话号码去其它表中查询该用户的头像ID。

利用电话号码查询头像ID最简单的方式就是利用phone_lookup表的ContentProvider来进行查询。

该ContentProvider支持的Uri格式为:Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, 电话号码);

完整的利用电话号码查询头像id的代码实例为:
protected static int getPhotoIdByNumber(Context context,String number) {
    int photoId = 0;
    //利用phone_lookup数据表所对应的ContentProvider进行查询
    ContentResolver cr = context.getContentResolver();
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, number);
    Cursor c = cr.query(uri , new String[]{PhoneLookup.PHOTO_ID}, null, null, null);
    //如果提供的电话号码确实是有头像的
    if(c.moveToNext()){
        photoId = c.getInt(0);
    }
        c.close();
        return photoId;
    }


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值