自定义搜索在Android的联系人自定义、联系人、Android

2023-09-04 03:06:26 作者:不是你的微信好友

我想在Android的联系人自定义搜索。

I want to custom search in contacts in android.

例如,我想联系人与每个联系人号码结束555 让我怎么能做到这一点?

For example, i want contacts that each contact number end with 555 so how can i do that?

推荐答案

获取所有联系人和手动搜索使用code。

Get all contacts and search manually using code.

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的联系人格式与空间 - 和()。它可能是更好的检查之前将其删除。使用

Also Android contacts are formatted with spaces,- and (). it may be better to remove them before checking. Using

phone.replace("-", "").replace("(", "").replace(")", "").replace(".", "").replace(" ", "");

另请参见here.