最近給別人做Android的項目,其中有關於手機聯系人的那一塊,要求查看聯系人的信息(手機號碼,名字,所在群組,家庭號碼,家庭地址,電子郵箱,備注,公司,工作號碼等)。在此分享下學習android手機聯系人數據庫的知識。如有遺漏和錯誤,望請教。
Android手機聯系人數據庫分析 下載:
免費下載地址在 http://linux.linuxidc.com/
用戶名與密碼都是www.linuxidc.com
具體下載目錄在 /2013年資料/12月/24日/Android手機聯系人數據庫分析
下載方法見 http://www.linuxidc.com/Linux/2013-07/87684.htm
運行結果圖如下:
上面是每個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());
}
class ViewHolder {
TextView name, note;
TextView number, address;
TextView group, company;
TextView email, workTel;
TextView fnumber;
}
class Item {
String name; // 姓名
String number; // 電話號碼
String group=""; // 群組
String note; // 備注
String address; // 家庭地址
String fnumber; // 家庭號碼
String company; // 所在公司
String workTel; // 工作號碼
String email; // 郵箱
}
group = "" ,是因為某些人可能屬於多個分組,為了在拼接字符串時不會有null所以賦予""
在進行一系列查詢的時候應該注意一點的就是:Contacts表中_ID是唯一的,也是和其他表關聯的,也就是說,得到這個Id後,其他信息都可以通過它得到。那麼反過來利用其他值(如:手機號)來得到這個id後,也就可以得到其他相關的信息了。