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

在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. 头像图片任意截取
  2. android实现百度地图点击覆盖物(MyLocationOverlay)弹出自定义弹
  3. 显示所有APP的进程详细信息(进程ID、进程所在UID、进程占用内存、
  4. android sqlite查询数据表的字段与相关属性
  5. android在只拥有第三方apk的情况下在自己app中打开第三方app
  6. Android(安卓)MMS 数据存取数据表
  7. android adt 最新下载地址
  8. android adt 最新下载地址23.03
  9. Android仿微信QQ群头像生成

随机推荐

  1. JDBC连接并使用mysql数据库
  2. SQL Server 2008使用sproc中的函数
  3. 如何在ACCESS中接收SQL SERVER返回结果集
  4. 脚本与SQL Server中的表并发运行
  5. logstash-jdbc-input与mysql数据库同步
  6. 求查询成绩表中两门科成绩90分以上的学生
  7. SQL Server Reporting Services图表查询
  8. Python将MySQL表数据写入excel
  9. SQL当执行插入操作时,字符串含有''的时候
  10. mybatis框架的进一步封装