更新脏的原始联系人

时间:2015-09-04 01:19:13

标签: java android android-contacts contactscontract

我创建了一个原始联系人,如果联系人有任何更改,则会更新。 Peradventure,用户在下次同步之前删除原始联系人,联系人有一个脏标志。

从我的实施中,在这样的场景中,我首先清除标志并更新联系人。

private static void clearDirtyFlag(Context context, long rawContactId,
                                   BatchOperation batchOperation) {
    final ContactOperations contactOp =
            ContactOperations.updateExistingContact(context, rawContactId,
                    batchOperation);

    final Uri uri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId);
    contactOp.updateDirtyFlag(false, uri);
}

private static void updateContact(Context context,
                                  ContentResolver resolver, String accountName, User user,
                                  long rawContactId, BatchOperation batchOperation) {
    Uri uri;
    String cellPhone = null;
    String otherPhone = null;
    String email = null;

    final Cursor c =
            resolver.query(Data.CONTENT_URI, DataQuery.PROJECTION,
                    DataQuery.SELECTION,
                    new String[]{String.valueOf(rawContactId)}, null);
    final ContactOperations contactOp =
            ContactOperations.updateExistingContact(context, rawContactId,
                    batchOperation);

    try {
        while (c.moveToNext()) {

            final long id = c.getLong(DataQuery.COLUMN_ID);
            final String mimeType = c.getString(DataQuery.COLUMN_MIMETYPE);
            uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);

            if (mimeType.equals(StructuredName.CONTENT_ITEM_TYPE)) {
                final String lastName =
                        c.getString(DataQuery.COLUMN_FAMILY_NAME);
                final String firstName =
                        c.getString(DataQuery.COLUMN_GIVEN_NAME);
                contactOp.updateName(uri, firstName, lastName, user
                        .getName());
            } else if (mimeType.equals(Phone.CONTENT_ITEM_TYPE)) {
                final int type = c.getInt(DataQuery.COLUMN_PHONE_TYPE);

                if (type == Phone.TYPE_MOBILE) {
                    cellPhone = c.getString(DataQuery.COLUMN_PHONE_NUMBER);
                    contactOp.updatePhone(cellPhone, user.getCellPhone(),
                            uri);
                } else if (type == Phone.TYPE_OTHER) {
                    otherPhone = c.getString(DataQuery.COLUMN_PHONE_NUMBER);
                    contactOp.updatePhone(otherPhone, user.getCellPhone(),
                            uri);
                }
            } else if (Data.MIMETYPE.equals(Email.CONTENT_ITEM_TYPE)) {
                email = c.getString(DataQuery.COLUMN_EMAIL_ADDRESS);
                // contactOp.updateEmail(user.getEmail(), email, uri);

            }
        } // while
    } finally {
        c.close();
    }


}

问题是联系人永远不会更新,因为raw_contact_id不再存在。 我是以正确的方式实现这一点,还是以何种方式更新脏的原始联系人。感谢

1 个答案:

答案 0 :(得分:0)

只有SyncAdapter(原始联系人所属的帐户应用程序,例如Facebook,Google,Corporate)才能访问已删除的联系人。没有其他应用程序将能够访问该原始联系人,因为在服务器上删除它之后必须由同步适配器删除它。如果您正在编写同步适配器,则已在URI中传递CALLER_IS_SYNCADAPTER。