本文主要介紹android中怎樣獲取系統的聯繫人數據 首先打開模擬器 點擊聯繫人圖標按鈕 說明系統聯繫人資料庫是空的,打開File explorer,找到data/data下麵的文件夾: 將contacts2.db文件導出,添加到sqlite中的時候報錯: 點擊“ok”忽略之 表結構相當的複雜,首先 ...
本文主要介紹android中怎樣獲取系統的聯繫人數據
首先打開模擬器
點擊聯繫人圖標按鈕
說明系統聯繫人資料庫是空的,打開File explorer,找到data/data下麵的文件夾:
將contacts2.db文件導出,添加到sqlite中的時候報錯:
點擊“ok”忽略之
表結構相當的複雜,首先搞清楚其中的三張表
* data表 保存聯繫人的數據
* raw_contacts表 保存聯繫人的id contact_id
* mimetypes表 保存聯繫人數據的類型
接下來通過模擬器添加聯繫人
通過sqlite刷新contacts2.db,忽略提示錯誤
獲取聯繫人步驟
1. 查詢raw_contacts表,把聯繫人的id取出來
2. 根據聯繫人的id,查詢data表,把這個id的數據取出來
3. 根據mimetype區分數據的類型
接下來通過代碼實現
package com.wuyudong.getcontacts; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.ContentResolver; import android.database.Cursor; import android.view.Menu; import android.view.View; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } /** * 獲取全部的聯繫人 * * @param view */ public void click(View view) { ContentResolver resolver = getContentResolver(); // 1.查詢raw_contacts表,把聯繫人的id取出來 Uri uri = Uri.parse("content://com.android.contacts/raw_contacts"); Uri datauri = Uri.parse("content://com.android.contacts/data"); Cursor cursor = resolver.query(uri, new String[] { "contact_id" }, null, null, null); while (cursor.moveToNext()) { String id = cursor.getString(0); System.out.println("聯繫人id: " + id); // 2.根據聯繫人的id,查詢data表,把這個id的數據取出來 // 系統api查詢data表的時候不是真正的查詢的data表,而是查詢data表的視圖 Cursor dataCursor = resolver.query(datauri, new String[] { "data1", "mimetype" }, "raw_contact_id=?", new String[] { id }, null); while (dataCursor.moveToNext()) { String data1 = dataCursor.getString(0); System.out.println("data1=" + data1); String mimetype = dataCursor.getString(1); System.out.println("mimetype=" + mimetype); } dataCursor.close(); } cursor.close(); } }
添加許可權:android.permission.READ_CONTACTS
運行後列印相關的結果:
06-18 10:59:41.556: I/System.out(2127): 聯繫人id: 1
06-18 10:59:41.636: I/System.out(2127): data1=110
06-18 10:59:41.636: I/System.out(2127): mimetype=vnd.android.cursor.item/phone_v2
06-18 10:59:41.646: I/System.out(2127): [email protected]
06-18 10:59:41.646: I/System.out(2127): mimetype=vnd.android.cursor.item/email_v2
06-18 10:59:41.646: I/System.out(2127): data1=Wuyudong
06-18 10:59:41.646: I/System.out(2127): mimetype=vnd.android.cursor.item/name
通過上面的列印信息,可以修改程式
package com.wuyudong.getcontacts; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.ContentResolver; import android.database.Cursor; import android.view.Menu; import android.view.View; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } /** * 獲取全部的聯繫人 * * @param view */ public void click(View view) { ContentResolver resolver = getContentResolver(); // 1.查詢raw_contacts表,把聯繫人的id取出來 Uri uri = Uri.parse("content://com.android.contacts/raw_contacts"); Uri datauri = Uri.parse("content://com.android.contacts/data"); Cursor cursor = resolver.query(uri, new String[] { "contact_id" }, null, null, null); while (cursor.moveToNext()) { String id = cursor.getString(0); System.out.println("聯繫人id: " + id); // 2.根據聯繫人的id,查詢data表,把這個id的數據取出來 // 系統api查詢data表的時候不是真正的查詢的data表,而是查詢data表的視圖 Cursor dataCursor = resolver .query(datauri, new String[] { "data1", "mimetype" }, "raw_contact_id=?", new String[] { id }, null); while (dataCursor.moveToNext()) { String data1 = dataCursor.getString(0); String mimetype = dataCursor.getString(1); if ("vnd.android.cursor.item/name".equals(mimetype)) { System.out.println("姓名=" + data1); } else if ("vnd.android.cursor.item/email_v2".equals(mimetype)) { System.out.println("郵箱=" + data1); } else if ("vnd.android.cursor.item/phone_v2".equals(data1)) { System.out.println("電話=" + data1); } else if ("vnd.android.cursor.item/im".equals(mimetype)) { System.out.println("QQ=" + data1); } } dataCursor.close(); } cursor.close(); } }
運行後列印相關的結果:
06-18 11:17:57.312: I/System.out(2380): 聯繫人id: 1
06-18 11:17:57.452: I/System.out(2380): 郵箱[email protected]
06-18 11:17:57.452: I/System.out(2380): 姓名=Wuyudong
06-18 11:17:57.452: I/System.out(2380): QQ=11111111
06-18 11:17:57.482: I/System.out(2380): 聯繫人id: 2
06-18 11:17:57.572: I/System.out(2380): QQ=32423423422
06-18 11:17:57.572: I/System.out(2380): 郵箱[email protected]
06-18 11:17:57.572: I/System.out(2380): 姓名=Zhangsan
最後將這些操作封裝成api,便於以後使用
新建ContactInfoParser.java文件
package com.wuyudong.getcontacts.service; import java.util.ArrayList; import java.util.List; import com.wuyudong.getcontacts.domain.ContactInfo; import android.content.ContentResolver; import android.content.Context; import android.database.Cursor; import android.net.Uri; public class ContactInfoParser { /** * 獲取系統全部聯繫人的api方法 * * @param context * @return */ public static List<ContactInfo> findAll(Context context) { ContentResolver resolver = context.getContentResolver(); // 1.查詢raw_contacts表,把聯繫人的id取出來 Uri uri = Uri.parse("content://com.android.contacts/raw_contacts"); Uri datauri = Uri.parse("content://com.android.contacts/data"); List<ContactInfo> infos = new ArrayList<ContactInfo>(); Cursor cursor = resolver.query(uri, new String[] { "contact_id" }, null, null, null); while (cursor.moveToNext()) { String id = cursor.getString(0); if (id != null) { System.out.println("聯繫人id: " + id); ContactInfo info = new ContactInfo(); info.setId(id); // 2.根據聯繫人的id,查詢data表,把這個id的數據取出來 // 系統api查詢data表的時候不是真正的查詢的data表,而是查詢data表的視圖 Cursor dataCursor = resolver.query(datauri, new String[] { "data1", "mimetype" }, "raw_contact_id=?", new String[] { id }, null); while (dataCursor.moveToNext()) { String data1 = dataCursor.getString(0); String mimetype = dataCursor.getString(1); if ("vnd.android.cursor.item/name".equals(mimetype)) { System.out.println("姓名=" + data1); info.setName(data1); } else if ("vnd.android.cursor.item/email_v2" .equals(mimetype)) { System.out.println("郵箱=" + data1); info.setEmail(data1); } else if ("vnd.android.cursor.item/phone_v2".equals(data1)) { System.out.println("電話=" + data1); info.setPhone(data1); } else if ("vnd.android.cursor.item/im".equals(mimetype)) { System.out.println("QQ=" + data1); info.setQq(data1); } } infos.add(info); System.out.println("-------"); dataCursor.close(); } } cursor.close(); return infos; } }
新建ContactInfo.java
package com.wuyudong.getcontacts.domain; public class ContactInfo { private String name; private String id; private String phone; private String email; private String qq; @Override public String toString() { return "ContactInfo [name=" + name + ", id=" + id + ", phone=" + phone + ", email=" + email + ", qq=" + qq + "]"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getQq() { return qq; } public void setQq(String qq) { this.qq = qq; } }
最後調用相關api
package com.wuyudong.getcontacts; import java.util.List; import com.wuyudong.getcontacts.domain.ContactInfo; import com.wuyudong.getcontacts.service.ContactInfoParser; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.ContentResolver; import android.database.Cursor; import android.view.Menu; import android.view.View; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } /** * 獲取全部的聯繫人 * * @param view */ public void click(View view) { List<ContactInfo> infos = ContactInfoParser.findAll(this); for (ContactInfo info : infos) { System.out.println(info.toString()); } } }