【发布时间】: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 联系人,如果他选择仅显示电话联系人、仅显示电话联系人等...
【问题讨论】:
-
所以,您的目标是让应用程序仅显示用户在设备中保存的那些联系人?是吗?
-
实际上,它只显示用户决定在他的通讯录中显示的那些联系人。你得到了“显示选项”,在那里你得到了“选择要显示的联系人”。我想得到完全过滤的联系人。
-
不,我刚刚删除了评论