大佬教程收集整理的这篇文章主要介绍了浅析Android手机卫士读取联系人,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
推荐阅读:
获取ContentResolver内容解析器对象,通过getContentResolver()方法
调用ContentResolver对象的query()方法,得到raw_contacts表里面的数据,得到cursor对象
参数:Uri对象,字段String数组
获取Uri对象,通过Uri.parse(“content://com.android.contacts/raw_contacts”)方法,
while循环cursor对象,条件是cursor对象moveToNext()方法为真
调用cursor对象的getString()方法,参数是索引
判断不为null,查询另一张表
调用ContentResolver对象的query()方法,得到data表里面的数据,得到cursor对象
参数:Uri对象,字段String[]数组(data1,mimetype),条件String,条件值String[]数组(contact_id)
Uri对象是Uri.parse(“content://com.android.contacts/data”)
循环和上面一样
姓名对应的类型是vnd.android.cursor.item/name
电话对应的类型是vnd.android.cursor.item/phone_v2
需要权限,android.permisssion.READ_CONTACTS
调用ListView对象的setAdapter()方法,分配数据到视图,参数是Adapter对象
通过new SimpleAdapter()来获得Adapter对象
参数:上下文,数据集合,布局资源,字段String[]数组,控件int[] id数组
package com.qingguow.mobilesafe.utils; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.content.ContentResolver; import android.content.Context; import android.database.cursor; import android.net.Uri; /** * 读取手机联系人 * @author taoshihan * */ public class PhoneContactsUtil { public static List<Map<String,String>> getContacts(Context context){ ContentResolver resolver=context.getContentResolver(); Uri uri=Uri.parse("content://com.android.contacts/raw_contacts"); Uri dataUri=Uri.parse("content://com.android.contacts/data"); List<Map<String,String>> contacts=new ArrayList<Map<String,String>>(); //循环联系人表 cursor cursor=resolver.query(uri,new String[]{"contact_id"},null,null); while(cursor.moveToNext()){ String id=cursor.getString(cursor.getcolumnIndex("contact_id")); if(id!=null){ Map<String,String> contact=new HashMap<String,String>(); //查找数据表 cursor datacursor=resolver.query(dataUri,new String[]{"data1","mimetype"},"raw_contact_id=?",new String[]{iD},null); while(datacursor.moveToNext()){ String data1=datacursor.getString(datacursor.getcolumnIndex("data1")); String mimetype=datacursor.getString(datacursor.getcolumnIndex("mimetype")); System.out.println("data1:"+data1+",mimetype:"+mimetypE); if(mimetype.equals("vnd.android.cursor.item/name")){ contact.put("name",data1); }else if(mimetype.equals("vnd.android.cursor.item/phone_v2")){ contact.put("phone",data1); } } contacts.add(contact); datacursor.close(); } } cursor.close(); return contacts; } }
以上内容是小编给大家介绍的android 手机卫士读取联系人的相关介绍,希望对大家有所帮助!
以上是大佬教程为你收集整理的浅析Android手机卫士读取联系人全部内容,希望文章能够帮你解决浅析Android手机卫士读取联系人所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。