uri = Uri.parse(“content://com.android.contacts/contacts/”
- contractID + “/data”);
Cursor cursor1 = resolver.query(uri, new String[] { “mimetype”,
“data1”, “data2” }, null, null, null);
while (cursor1.moveToNext()) {
String data1 = cursor1.getString(cursor1
.getColumnIndex(“data1”));
String mimeType = cursor1.getString(cursor1
.getColumnIndex(“mimetype”));
if (“vnd.android.cursor.item/name”.equals(mimeType)) { // 是姓名
contact.setName(data1);
sb.append(“,name=” + data1);
} else if (“vnd.android.cursor.item/email_v2”.equals(mimeType)) { // 邮箱
if (!TextUtils.isEmpty(data1)) {
contact.setEmail(data1);
}
contacts.add(contact);
sb.append(“,email=” + data1);
} else if (“vnd.android.cursor.item/phone_v2”.equals(mimeType)) { // 手机
contact.setNumber(data1.replaceAll(“-”, “”));
sb.append(“,phone=” + data1);
}
}
contacts.add(contact);
cursor1.close();
Log.i(“wwj”, sb.toString());
}
cursor.close();
return contacts;
}
关于模糊匹配,我真的有点头大,我想到的是用正则表达式来做,但需要用到第三方类库pinyin4j.jar,我试了一下,效率都点低,明显感觉有点慢了,假如你的通讯录的量比较大,用这个来做确实不合时宜,或许可以进行优化的地方。
定义一个工具类
package com.search;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.text.TextUtils;
public class BaseUtil {
public final static String[] PHONES_PROJECTION = new String[] {
Phone.DISPLAY_NAME, Phone.NUMBER };
public static String STRS[] = { “”, “”, “[abc]”, “[def]”, “[ghi]”, “[jkl]”,
“[mno]”, “[pqrs]”, “[tuv]”, “[wxyz]” };
/**
-
将字符串中的中文转化为拼音,其他字符不变
-
@param inputString
-
@return
*/
public static String getPingYin(String inputString) {
if (TextUtils.isEmpty(inputString)) {
return “”;
}
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
format.setVCharType(HanyuPinyinVCharType.WITH_V);
char[] input = inputString.trim().toCharArray();
String output = “”;
try {
for (int i = 0; i < input.length; i++) {
if (java.lang.Character.toString(input[i]).matches(
“[\u4E00-\u9FA5]+”)) {
String[] temp = PinyinHelper.toHanyuPinyinStringArray(
input[i], format);
if (temp == null || TextUtils.isEmpty(temp[0])) {
continue;
}
output += temp[0].replaceFirst(temp[0].substring(0, 1),
temp[0].substring(0, 1).toUpperCase());
} else
output += java.lang.Character.toString(input[i]);
}
} catch (Exception e) {
e.printStackTrace();
}
return output;
}
}
/**
-
按号码-拼音搜索联系人