【问题标题】:Get only numbers shown in the android contactbook仅获取 android 通讯录中显示的号码
【发布时间】:2015-01-17 17:31:23
【问题描述】:

我想根据用户选择在他的通讯录中显示的联系人来过滤我从 Android 获得的号码。 (例如,当排除所有其他联系人时,仅保存在设备上的联系人)

我读到here 你可以通过使用 Uri queryUri = ContactsContract.Contacts.CONTENT_URI;

我使用以下代码读取联系人,我总是得到每个联系人、电话、SIM 卡等。

    //https://stackoverflow.com/questions/16651609/how-to-display-phone-contacts-only-exclude-sim-contacts
    // http://www.higherpass.com/Android/Tutorials/Working-With-Android-Contacts/1/
    ContentResolver cr = currentActivity.getContentResolver();

    Uri queryUri = ContactsContract.Contacts.CONTENT_URI;
    Cursor cur = cr.query(queryUri,
            null, null, null, null);
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {  //Are there still contacts?
            //See if the contact has at least one number
            if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {

                String id   = cur.getString( cur.getColumnIndex(ContactsContract.Contacts._ID) );
                String name = cur.getString( cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME) );
                ArrayList<String> numbers = new ArrayList<String>();

                //Read numbers:
                Cursor pCur = cr.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                        new String[]{id}, null);
                while (pCur.moveToNext()) {
                   numbers.add( pCur.getString(
                           pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))  );
                   Log.e("Contactbook", "The latest added number is: " + numbers.get(numbers.size()-1) );
                }
                pCur.close();
            }


        }
    }

我错过了什么?此代码仍然为我提供了日志中的 SIM 和电话联系人。

编辑:为了澄清,在通讯录中你得到了“显示选项”。那里有“选择要显示的联系人”选项,我想阅读根据用户选择显示的联系人。因此,如果用户选择仅显示 SIM 联系人、只读 SIM 联系人,如果他选择仅显示电话联系人、仅显示电话联系人等...

【问题讨论】:

  • 所以,您的目标是让应用程序显示用户在设备中保存的那些联系人?是吗?
  • 实际上,它只显示用户决定在他的通讯录中显示的那些联系人。你得到了“显示选项”,在那里你得到了“选择要显示的联系人”。我想得到完全过滤的联系人。
  • 不,我刚刚删除了评论

标签: android contacts


【解决方案1】:

尝试以下“选择”。

String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = ?";

来自 Android 文档:

public static final String IN_VISIBLE_GROUP: An indicator of whether this contact is supposed to be visible in the UI. "1" if the contact has at least one raw contact that belongs to a visible group; "0" otherwise.

这应该是您在查询 API 中的“选择”参数。

更新:我在 Android-2.3 上尝试了以下代码(我知道它是旧设备,但现在我没有更新的设备)。

final ContentResolver cr = getContentResolver();
    String[] projection = new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME };
    String selection =  ContactsContract.Contacts.IN_VISIBLE_GROUP + " = ?" ;
    String[] Args = { "1" };
    final Cursor contacts = cr.query(
                            ContactsContract.Contacts.CONTENT_URI, projection,
                            selection,Args , 
                            null);

这可能是一个很长的操作(取决于联系人的数量),因此您应该为此使用 CursorLoader 类(一个查询 ContentResolver 并返回 Cursor 的加载器)。

cursorLoader.loadInBackground(); 

这将在工作线程上调用以执行实际加载并返回加载操作的结果。

【讨论】:

  • 字符串选择 = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = ?";光标 cur = cr.query(queryUri, null, selection, null, null);以这种方式更改了它,但现在我根本没有任何联系方式。
  • String[] 投影 = { ContactsContract.Contacts.DISPLAY_NAME,ContactsContract.Contacts._ID };
【解决方案2】:

您可以轻松地为每种联系人创建两个数组

 ArrayList<String> simContacts = new ArrayList<>();
 //get all sim contacts
Uri simUri = Uri.parse("content://icc/adn");
    Cursor cursorSim = getContext().getContentResolver().query(simUri, null, null, null, null);

    while (cursorSim.moveToNext()) {
        simContacts.add(cursorSim.getString(cursorSim.getColumnIndex("name")));
    } 

}

 ArrayList<String> allContacts = new ArrayList<>(); 
//get all contacts
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
            null, null, null, null);
         if (cursor != null && cursor.getCount() > 0) {
 while (!cursor.isAfterLast()) {

String phoneNumber = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
String displayNameColumn = Utils.hasHoneycomb() ? ContactsContract.Contacts.DISPLAY_NAME_PRIMARY : ContactsContract.Contacts.DISPLAY_NAME;
                String displayName = cursor.getString(cursor.getColumnIndexOrThrow(displayNameColumn));
//check if simContacts Array contains this particular name
if (!simContacts.contains(displayNameColumn){
allContacts.add(displayNameColumn);
}
 }
}

这只是一个工作示例,当然您可以根据需要进行修改。您可以解析更多联系人字段并进行更复杂的查询。

【讨论】:

    猜你喜欢
    • 2013-11-14
    • 2012-10-27
    • 1970-01-01
    • 2011-07-19
    • 1970-01-01
    • 2012-11-14
    • 1970-01-01
    • 2012-12-10
    • 2014-09-20
    相关资源
    最近更新 更多