简体   繁体   English

只获取显示在android通讯录中的号码

[英]Get only numbers shown in the android contactbook

I want to filter what numbers I am getting from Android based on which contacts the user chose to display in his contact book. 我想根据用户选择在联系人簿中显示的联系人来过滤从Android获得的号码。 (For example only contacts saved on the device when all other contacts are excluded) (例如,当排除所有其他联系人时,仅保存在设备上的联系人)

I read here that you can do this by using Uri queryUri = ContactsContract.Contacts.CONTENT_URI; 在这里阅读 ,您可以使用Uri来做到这一点queryUri = ContactsContract.Contacts.CONTENT_URI;

I use following code to read the contacts and I allways get every contact, phone, SIM, etc.. 我使用以下代码读取联系人,并始终获得每个联系人,电话,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();
            }


        }
    }

What am I missing? 我想念什么? This code still gives me both SIM and phone contacts to the log. 这段代码仍将我和SIM卡的联系方式同时发送给我。

Edit: To clarifify, in the contactbook you got the "Display options". 编辑:为澄清起见,在通讯录中有“显示选项”。 In there ist the "select contacts to display"-option, and I want to read the contacts that are shown based on the users choice there. 在“选择要显示的联系人”选项中,我想阅读根据用户在那里选择显示的联系人。 So if a user choses to show only SIM-contacts, read only SIM-contacts, if he choses to only show Phone-Contacts, show onyl phone contacts etc... 因此,如果用户选择仅显示SIM卡联系人,则仅读取S​​IM卡联系人,如果用户选择仅显示电话联系人,显示在线电话联系人等...

Try with following "selection". 尝试执行以下“选择”。

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

From Android docs: 从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.

This should be your "selection" argument in query API. 这应该是查询API中的“选择”参数。

Update: I tried below code on Android-2.3(I know it is old device, but right now Don't have newer device with me). 更新:我在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);

This could be a long operation (depending on no. of contacts), hence you should use CursorLoader class (A loader that queries the ContentResolver and returns a Cursor) for this. 这可能是一个较长的操作(取决于联系人的数量),因此您应为此使用CursorLoader类(查询ContentResolver并返回Cursor的加载器)。

cursorLoader.loadInBackground(); 

This will be called on a worker thread to perform the actual load and to return the result of the load operation. 这将在工作线程上调用以执行实际加载并返回加载操作的结果。

You can easily create two Arrays for each kind of contacts 您可以为每种联系人轻松创建两个数组

 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);
}
 }
}

This is just a working example,of course you can modify to your needs.You can parse more contact fields and make more complication queries. 这只是一个有效的示例,您当然可以根据需要进行修改。您可以解析更多的联系人字段并进行更多的复杂查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM