读取手机联系人2中方法
来源:互联网 发布:大专软件学校 编辑:程序博客网 时间:2024/04/28 00:44
读取手机联系人2中方法(内容解析者)
基础方法
public class ContactInfoUtils { /** * 获取所有的联系人信息 */ public static List<ContactInfo> getAllContactInfos(Context context) { List<ContactInfo> infos = new ArrayList<ContactInfo>(); ContentResolver resolver = context.getContentResolver(); // 查询raw_contact表 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); if (id != null) { ContactInfo info = new ContactInfo(); // 查询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)) { info.setName(data1); } else if ("vnd.android.cursor.item/im".equals(mimetype)) { info.setQq(data1); } else if ("vnd.android.cursor.item/email_v2" .equals(mimetype)) { info.setEmail(data1); } else if ("vnd.android.cursor.item/phone_v2" .equals(mimetype)) { info.setPhone(data1); } } datacursor.close(); infos.add(info); } } cursor.close(); return infos; }}
API方法
public class ContactProvider {/** * 获取系统所有联系人 * @param context * @return List<ContactBean> */public static List<ContactBean> getAllContacts(Context context) { List<ContactBean> list = new ArrayList<ContactBean>(); // 数据查询--》系统的联系人 ContentResolver cr = context.getContentResolver(); // 通过号码查询 Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;// 要查询数据对应的uri地址 String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,// 名称 ContactsContract.CommonDataKinds.Phone.NUMBER,// 号码 ContactsContract.CommonDataKinds.Phone.CONTACT_ID // 联系人的id };// 要查询的列 String selection = null;// 查询的条件 String[] selectionArgs = null;// 查询条件对应的参数 String sortOrder = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " desc";// 查询的排序 Cursor cursor = cr.query(uri, projection, selection, selectionArgs, sortOrder); if (cursor != null) { while (cursor.moveToNext()) { String name = cursor.getString(0); String number = cursor.getString(1); long contactId = cursor.getLong(2); ContactBean bean = new ContactBean(); bean.name = name; bean.number = number; bean.contactId = contactId; list.add(bean); } cursor.close(); } return list;}/** * 得到联系人的头像 * @param context:上下文 * @param contactId:联系人id * @return:bitmap头像 */public static Bitmap getContactPhoto(Context context, long contactId) { ContentResolver cr = context.getContentResolver(); // content://contacts //所有联系人 // content://contacts/1 //具体联系人 // ContactsContract.Contacts.CONTENT_URI;//所有联系人的uri Uri contactUri = Uri.withAppendedPath( ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactId)); InputStream stream = ContactsContract.Contacts .openContactPhotoInputStream(cr, contactUri); // 将流转换为bitmap return BitmapFactory.decodeStream(stream);}
}
0 0
- 读取手机联系人2中方法
- 分页读取手机联系人
- android 读取手机联系人
- 方便快捷读取手机联系人
- 读取手机联系人
- 读取手机联系人
- 读取手机联系人信息
- 读取手机的联系人
- 读取手机联系人
- 360-读取手机联系人
- android读取手机联系人
- 读取手机联系人
- 读取手机联系人信息
- 读取手机联系人
- 读取手机联系人信息
- 读取手机联系人
- 读取手机联系人简单方法(id,姓名,手机号)
- Android 读取手机联系人信息
- 史上最简单的 Spring MVC 教程(八)
- 优化ListView数据适配器套路
- P1605 迷宫
- QSpin and QSlider QStackedWidget and QComboBox学习笔记4
- 机器学习中的范数规则化之(一)L0、L1与L2范数
- 读取手机联系人2中方法
- 设备管理员套路
- 手势识别器套路
- poj初期数据结构
- 系统Manager套路
- 一键锁屏和一键卸载套路
- P1019 单词接龙
- 电话打入和打出的区别
- 2017年美赛D题---基于排队仿真的机场安检瓶颈问题