且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Android中识别本地电话联系人和SYNC电话联系人?

更新时间:2022-03-27 23:22:51

我正在发布此答案供将来使用。我们可以使用 RawContacts.SOURCE_ID

I am posting this answer for future use. We can differentiate the local phone contacts and the sync contacts by using the field called RawContacts.SOURCE_ID

来区分本地电话联系人和同步联系人。 此处

It is described here

SOURCE_ID

读/写

唯一标识此行为其源帐户的字符串。通常,它是在插入原始触点时设置的,此后再也不会更改。一个值得注意的例外是一个新的原始联系人:它将具有一个帐户名和类型(可能还有一个数据集),但是没有源ID。这向同步适配器指示需要在服务器端创建一个新联系人,并将其ID存储在电话上的相应SOURCE_ID字段中。

SOURCE_ID
read/write
String that uniquely identifies this row to its source account. Typically it is set at the time the raw contact is inserted and never changed afterwards. The one notable exception is a new raw contact: it will have an account name and type (and possibly a data set), but no source id. This indicates to the sync adapter that a new contact needs to be created server-side and its ID stored in the corresponding SOURCE_ID field on the phone.

示例代码如下,它给出了同步联系人的ID,而其他联系人则为null。

The sample code is follows, it gives the id for sync contacts and null for others.

private void testContact() {
        StringBuffer output = new StringBuffer();
        ContentResolver resolver = getContentResolver();
        Cursor contacts = resolver.query(Contacts.CONTENT_URI, null,
                Contacts.HAS_PHONE_NUMBER + " != 0", null, Contacts._ID
                        + " ASC");
        Cursor data = resolver.query(Data.CONTENT_URI, null, Data.MIMETYPE
                + "=? OR " + Data.MIMETYPE + "=?", new String[]{
                Email.CONTENT_ITEM_TYPE, Phone.CONTENT_ITEM_TYPE},
                Data.CONTACT_ID + " ASC");

        int idIndex = contacts.getColumnIndexOrThrow(Contacts._ID);
        int nameIndex = contacts.getColumnIndexOrThrow(Contacts.DISPLAY_NAME);
        int cidIndex = data.getColumnIndexOrThrow(Data.CONTACT_ID);
        int data1Index = data.getColumnIndexOrThrow(Data.DATA1);
        boolean hasData = data.moveToNext();

        while (contacts.moveToNext()) {
            long id = contacts.getLong(idIndex);
            Uri rawContactUri = 
                      ContentUris.withAppendedId(RawContacts.CONTENT_URI, id);

                    Uri entityUri =
                      Uri.withAppendedPath(rawContactUri, Entity.CONTENT_DIRECTORY);

                    Cursor c =
                      getContentResolver().query(
                        entityUri,
                        new String[] {
                                  RawContacts.ACCOUNT_NAME,
                          RawContacts.SOURCE_ID, Entity.DATA_ID, Entity.MIMETYPE, Entity.DATA1},
                        null, null, null);

                    try {
                         while (c.moveToNext()) {
                             String sourceId = c.getString(0);
                             if (!c.isNull(1)) {

                                 String source_id = c.getString(1);
                             try {
                                     output.append(c.getString(4)+sourceId+" "+source_id+"\n");

                                     //output.append(datas+ "Sync1 "+  c.getString(4)+" Sync2 "+  c.getString(5)+" Sync3"+  c.getString(6)+" Sync4 "+  c.getString(7)+"\n");
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                                 //decide here based on mimeType, see comment later
                             }
                         }
                    } finally {
                         c.close();
                    }

        }

        outputText.setText(output);
    }