本文主要介绍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): data1=wuyudong@wuyudong.com
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): 邮箱=wuyudong@wuyudong.com
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): 邮箱=wu@wuyudong.com
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()); } } }