Android通讯录模糊匹配搜索实现(号码、首字母、简拼、全拼),这份354页笔记的Android进阶知识+大厂高频面试题

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;

}

}

/**

  • 按号码-拼音搜索联系人

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值