最近给别人做Android的项目,其中有关于手机联系人的那一块,要求查看联系人的信息(手机号码,名字,所在群组,家庭号码,家庭地址,电子邮箱,备注,公司,工作号码等)。在此分享下学习android手机联系人数据库的知识。如有遗漏和错误,望请教。
Android手机联系人数据库分析 下载:
具体下载目录在 /2013年资料/12月/24日/Android手机联系人数据库分析
运行结果图如下:
上面是每个item对应的布局,右下角被挡住的两个分别为家庭号码,工作号码
哎,不同人信息不一样,长度也不一样啊,看起来不是很美观,
在群组中,因为所有人都在“所有联系人”组中,又在其他组中,故:查询组有多个。
还是先把核心代码贴出来
private void getData(){
HashMap<String, Item> list = new HashMap<String, Item>(100);
/*Cursor c = getContentResolver().query(Contacts.CONTENT_URI, new String[]{Contacts._ID}, null, null, null);
if(c.moveToFirst()){
int id = c.getColumnIndex(Contacts._ID);
}*/
// 姓名和手机号
Cursor c = getContentResolver().query(Phone.CONTENT_URI,
new String[]{Phone.DISPLAY_NAME, Phone.NUMBER, Phone.CONTACT_ID},
Phone.TYPE + "=?",
new String[]{Phone.TYPE_MOBILE+""}, null);
if(c.moveToFirst()){
int id = c.getColumnIndex(Phone.CONTACT_ID);
int name = c.getColumnIndex(Phone.DISPLAY_NAME);
int num = c.getColumnIndex(Phone.NUMBER);
do{
String cid = c.getString(id);
String cname = c.getString(name);
String cnum = c.getString(num);
Item remark = list.get(cid);
if(remark == null){
remark = new Item();
list.put(cid, remark);
}
remark.name = cname;
remark.number = cnum;
Log.i(TAG, "name:" + cname + " num:" + cnum);
}while(c.moveToNext());
}
c.close();
// 邮箱信息
c = getContentResolver().query(Email.CONTENT_URI,
new String[]{Email.DATA, Email.CONTACT_ID},
null, null, Email.CONTACT_ID + " asc");
if(c.moveToFirst()){
int id = c.getColumnIndex(Email.CONTACT_ID);
int em = c.getColumnIndex(Email.DATA);
do{
String cid = c.getString(id);
String email = c.getString(em);
Item remark = list.get(cid);
if(remark == null){
remark = new Item();
list.put(cid, remark);
}
remark.email = email;
Log.i(TAG, "email:" + email);
}while(c.moveToNext());
}
c.close();
Log.i(TAG, "start 工作号码");
// 工作号码
String[] projection = new String[] { Phone.NUMBER, Phone.CONTACT_ID};
c = getContentResolver().query(
Phone.CONTENT_URI, projection,
Phone.TYPE + "=?",
new String[]{Phone.TYPE_WORK + ""},
Phone.CONTACT_ID + " asc");
if(c.moveToFirst()){
int num = c.getColumnIndex(Phone.NUMBER);
int id = c.getColumnIndex(Phone.CONTACT_ID);
do{
String cid = c.getString(id);
String phone = c.getString(num);
Item remark = list.get(cid);
if(remark == null){
remark = new Item();
list.put(cid, remark);
}
remark.workTel = phone;
Log.i(TAG, "workTel:" + phone);
}while(c.moveToNext());
}
c.close();
// 公司名字
c = getContentResolver().query(
Data.CONTENT_URI, new String[]{Data.DATA1, Data.CONTACT_ID},
Data.MIMETYPE + "=?",
new String[]{Organization.CONTENT_ITEM_TYPE},
Data.CONTACT_ID + " asc");
if(c.moveToFirst()){
int data1 = c.getColumnIndex(Data.DATA1);
int id = c.getColumnIndex(Data.CONTACT_ID);
do{
String cid = c.getString(id);
String companyname = c.getString(data1);
Item remark = list.get(cid);
if(remark == null){
remark = new Item();
list.put(cid, remark);
}
remark.company = companyname;
Log.i(TAG, "company:" + companyname);
}while(c.moveToNext());
}
c.close(); c = null;
// 家庭住址信息
projection = new String[] { Data.DATA7,
Data.DATA10, Data.DATA4, Data.CONTACT_ID };
c = getContentResolver().query(
Data.CONTENT_URI, projection,
Data.MIMETYPE + "=? and " + StructuredPostal.TYPE + "=?" ,
new String[]{StructuredPostal.CONTENT_ITEM_TYPE, StructuredPostal.TYPE_HOME+""},
Data.CONTACT_ID + " asc");
if(c.moveToFirst()){
int data4 = c.getColumnIndex(Data.DATA4);
int data7 = c.getColumnIndex(Data.DATA7);
int data10 = c.getColumnIndex(Data.DATA10);
int id = c.getColumnIndex(Data.CONTACT_ID);
do{
String sData4 = c.getString(data4);
String sData7 = c.getString(data7);
String sData10 = c.getString(data10);
String cid = c.getString(id);
StringBuilder sb = new StringBuilder();
// 依次为国家、城市、街道
if(!TextUtils.isEmpty(sData10))
sb.append(sData10).append(";");
if(!TextUtils.isEmpty(sData7))
sb.append(sData7).append(";");
if(!TextUtils.isEmpty(sData4))
sb.append(sData4);
Item remark = list.get(cid);
if(remark == null){
remark = new Item();
list.put(cid, remark);
}
remark.address = sb.toString();
Log.i(TAG, "address:" + remark.address);
}while(c.moveToNext());
}
c.close();
// 家庭号码
projection = new String[] { Phone.NUMBER, Phone.CONTACT_ID};
c = getContentResolver().query(
Phone.CONTENT_URI, projection,
Phone.TYPE + "=?",
new String[]{Phone.TYPE_HOME + ""},
Phone.CONTACT_ID + " asc");
if(c.moveToFirst()){
int num = c.getColumnIndex(Phone.NUMBER);
int id = c.getColumnIndex(Phone.CONTACT_ID);
do{
String cid = c.getString(id);
String phone = c.getString(num);
Item remark = list.get(cid);
if(remark == null){
remark = new Item();
list.put(cid, remark);
}
remark.fnumber = phone;
Log.i(TAG, "family Phone:" + phone);
}while(c.moveToNext());
}
c.close();
// 备注
projection = new String[] { Data.DATA1, Data.CONTACT_ID};
c = getContentResolver().query(
Data.CONTENT_URI,
projection,
Data.MIMETYPE + "=?",
new String[]{Note.CONTENT_ITEM_TYPE},
Data.CONTACT_ID + " asc");
if(c.moveToFirst()){
int data1 = c.getColumnIndex(Data.DATA1);
int id = c.getColumnIndex(Data.CONTACT_ID);
do{
String cid = c.getString(id);
String note = c.getString(data1);
Item remark = list.get(cid);
if(remark == null){
remark = new Item();
list.put(cid, remark);
}
if(!TextUtils.isEmpty(note)){
remark.note = note;
Log.i(TAG, "note:" + note);
}
}while(c.moveToNext());
}
c.close();
// 对应的分组
ArrayList<String> gid = new ArrayList<String>(); // group Id
ArrayList<String> gname = new ArrayList<String>(); // group name
c = getContentResolver().query(Groups.CONTENT_URI,
new String[]{Groups.TITLE, Groups._ID}, null, null, null);
if(c.moveToFirst()){
int cc1 = c.getColumnIndex(Groups._ID);
int cc2 = c.getColumnIndex(Groups.TITLE);
do{
String id = c.getString(cc1);
String ti = c.getString(cc2);
gid.add(id);
gname.add(ti);
Log.i(TAG, ti);
}while(c.moveToNext());
}
c.close();
int len = gid.size(); /// 总共有多少个群组
c = getContentResolver().query(Data.CONTENT_URI,
new String[]{GroupMembership.CONTACT_ID, GroupMembership.GROUP_ROW_ID},
Data.MIMETYPE + "=?",
new String[]{GroupMembership.CONTENT_ITEM_TYPE}, null);
if(c.moveToFirst()){
int cc1 = c.getColumnIndex(GroupMembership.CONTACT_ID);
int cc2 = c.getColumnIndex(GroupMembership.GROUP_ROW_ID);// group ID
do{
String id = c.getString(cc1);
String groupId = c.getString(cc2);
Item remark = list.get(id);
if(remark == null){
remark = new Item();
list.put(id, remark);
}
for(int i = 0; i < len; i++){
if(gid.get(i).equals(groupId)){
remark.group += gname.get(i)+" ";// 一个人可能有多个群组
}
}
}while(c.moveToNext());
}
c.close();
gid = null;
gname = null;
Log.i(TAG, "end");
if(data.list.size() == 0)
data.list.clear();
Collection<Item> vs = list.values();
for(Item it : vs)
data.list.add(it);
Log.i(TAG, "size" + data.list.size());
}