android 搜索通讯录联系人
public static ArrayList searchByKeyword(String word , Context context) throws FileNotFoundException, IOException{ArrayList resultList = new ArrayList();Cursor c = context.getContentResolve
·
/**
* @Title: ContactsUtils.java
* @Package com.im_cmcc.common.utils
*/
package com.im_cmcc.common.utils;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import com.im_cmcc.bean.OrgNode;
/**
* @ClassName: ContactsUtils
* @Description: 手机自带通讯录工具类
* @author WQ
* @date 2014-7-10 上午9:48:54
*/
public class ContactsUtils {
/**
* @Description 根据传入的字符搜索通讯录
* @Title searchByKeyword
* @author WQ
* @date 2014-7-10 上午9:52:53
* @param word
* @param context
* @return
* @throws FileNotFoundException
* @throws IOException
*/
public static ArrayList<OrgNode> searchByKeyword(String word , Context context) throws FileNotFoundException, IOException{
ArrayList<OrgNode> resultList = new ArrayList<OrgNode>();
Cursor c = context.getContentResolver().query(Data.CONTENT_URI,
new String[]{Phone.DISPLAY_NAME,Phone.NUMBER,Phone.SORT_KEY_PRIMARY,Phone.RAW_CONTACT_ID , Phone.PHOTO_THUMBNAIL_URI , Phone.PHOTO_URI},
Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "' AND " + // 这里限制mineType
Phone.SORT_KEY_PRIMARY + " like upper('%"+ word +"%')"
, null,
null);
//SORT_KEY_PRIMARY 保存格式为: 假如联系人姓名为 张三 则 值为:ZHANG 张 SAN 三
while (c.moveToNext()) {
OrgNode n = new OrgNode();
n.setType(OrgNode.TYPE_BOOK);
n.setId(c.getString(3)); // RAW_CONTACT_ID
n.setName(c.getString(0)); //名称
n.setText(c.getString(1)); //电话号码
String thumb = c.getString(4); //PHOTO_THUMBNAIL_URI
if(null != thumb && !"".equals(thumb)){
Uri thumbUri = Uri.parse(thumb);
n.setThumb(MediaStore.Images.Media.getBitmap(context.getContentResolver(), thumbUri));
}
resultList.add(n);
}
return resultList;
}
}
注意 AndroidManifest.xml 需要这个权限
<uses-permission android:name="android.permission.READ_CONTACTS" />
更多推荐
已为社区贡献1条内容
所有评论(0)