我想在android中的联系人中自定义搜索。
例如,我想要每个联系人号码以555 结尾的联系人,那我该怎么办呢?
答案 0 :(得分:2)
获取所有联系人并使用代码手动搜索。
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
String phone = null;
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur .getColumnIndex(ContactsContract.Contacts._ID));
if (Integer .parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
Cursor pCur = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
while (pCur.moveToNext()) {
phone = pCur .getString(pCur .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
if(phone.endsWith( "555")
// store the number and id of the contact
}
pCur.close();
}
/*** Store id and phone in another variable(eg ArrayList,etc) so that it does not get lost in another iteration of while loop***/
}
}
此外,Android联系人的格式为空格, - 和()。在检查之前删除它们可能更好。使用
phone.replace("-", "").replace("(", "").replace(")", "").replace(".", "").replace(" ", "");
另见here。