android - 自定义搜索android中的联系人

标签 android sqlite android-contentprovider android-contacts

我想在 android 中自定义搜索联系人。

例如,我想要联系人每个联系人号码以 555 结尾,那么我该怎么做呢?

最佳答案

获取所有联系人并使用代码手动搜索。

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 .

关于android - 自定义搜索android中的联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26913315/

相关文章:

android - 始终可用的公用文件夹

android - 无法连接到 frida 服务器 : need Gadget to attach on jailed Android

android - 应用程序创建的线程如何被视为与应用程序的 ContentProvider 不同的应用程序?

Android 短信按号码阅读

iphone - 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“错误:无法打开数据库并显示消息“不是错误”。

android - 使用 ContentProviderClient 与 ContentResolver 访问内容提供者

android - 如何在高级 Recyclerview 库中插入项目

具有非默认语言环境的 ActionBar NullPointerException 的 Android 应用程序

Android studio 模拟器 sqlite db 文件

Perl 的 SQLite3 : {NAME} not working?