如何添加和更新联系人?

时间:2017-03-23 16:18:35

标签: java android

我尝试从数据库添加新联系人或更新现有联系人(按编号和更新名称和地址排序)。

我做了这样的事情:

    public void WritePhoneContact(String displayName, String number, String address,Context cntx)
{
    Context contetx     = cntx; 
    String strDisplayName   =  displayName; 
    String strNumber    =  number; 
    String strAddress = address;

    ArrayList<ContentProviderOperation> cntProOper = new ArrayList<ContentProviderOperation>();
    int contactIndex = cntProOper.size();

    Uri numberUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, Uri.encode(number));

    Cursor numberCursor = contetx.getContentResolver().query(numberUri, null, null, null, null);

    if (numberCursor != null && numberCursor.moveToFirst()) {

        cntProOper.add(ContentProviderOperation.newUpdate(RawContacts.CONTENT_URI)
                .withSelection(Phone.NUMBER,new String[]{number})
                .withValue(RawContacts.ACCOUNT_TYPE, null)
                .withValue(RawContacts.ACCOUNT_NAME, null).build());


        cntProOper.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
                .withSelection(Phone.NUMBER,new String[]{number})
                .withValueBackReference(Data.RAW_CONTACT_ID, contactIndex)
                .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
                .withValue(StructuredName.DISPLAY_NAME, strDisplayName) 
                .build());


        cntProOper.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
                .withSelection(Phone.NUMBER,new String[]{number})
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, contactIndex)
                .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
                .withValue(Phone.NUMBER, strNumber)
                .withValue(Phone.TYPE, Phone.TYPE_MOBILE).build()); 



        cntProOper.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
                .withSelection(Phone.NUMBER,new String[]{number})
                .withValueBackReference(Data.RAW_CONTACT_ID, contactIndex)
                .withValue(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE)
                .withValue(StructuredPostal.FORMATTED_ADDRESS, strAddress).build());
    } else {
        cntProOper.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
                .withValue(RawContacts.ACCOUNT_TYPE, null)
                .withValue(RawContacts.ACCOUNT_NAME, null).build());


        cntProOper.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                .withValueBackReference(Data.RAW_CONTACT_ID, contactIndex)
                .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
                .withValue(StructuredName.DISPLAY_NAME, strDisplayName)
                .build());


        cntProOper.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, contactIndex)
                .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
                .withValue(Phone.NUMBER, strNumber)
                .withValue(Phone.TYPE, Phone.TYPE_MOBILE).build());



        cntProOper.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                .withValueBackReference(Data.RAW_CONTACT_ID, contactIndex)
                .withValue(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE)
                .withValue(StructuredPostal.FORMATTED_ADDRESS, strAddress).build());
    }




    try
    {
        // We will do batch operation to insert all above data
        //Contains the output of the app of a ContentProviderOperation.
        //It is sure to have exactly one of uri or count set
        ContentProviderResult[] contentProresult = null;
        contentProresult = contetx.getContentResolver().applyBatch(ContactsContract.AUTHORITY, cntProOper); //apply above data insertion into contacts list
    }
    catch (RemoteException exp)
    {
        //logs;
    }
    catch (OperationApplicationException exp)
    {
        //logs
    }
}

但更新效果不佳。我认为订购和选择有问题。

0 个答案:

没有答案