Adding custom data to contacts in Android

廉价感情. 提交于 2020-01-03 05:22:28

问题


I want to add custom field to contacts that will tell me if the contact was marked in my aplication or not. First of all I want to make a function that will set my custom data to contact with given id, but the code that I try to use, don't work properly.

 public static final String             MIMETYPE_EMPLOYEE   = "vnd.android.cursor.item/employee";
public void addEmployee(String id){
            ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
            Uri newContactUri = null;
             ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
                      .withSelection(ContactsContract.Data._ID + "=?", new String[]{id})
                      .withValue(ContactsContract.Data.MIMETYPE, MIMETYPE_EMPLOYEE)
                      .withValue(ContactsContract.Data.DATA1, "yes") 
                      .build());

            try{
                ContentProviderResult[] res = act.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

                if (res!=null && res[0]!=null) {

                    newContactUri = res[0].uri; 
                    Log.d(LOG_TAG, "URI added contact:"+ newContactUri); //here it says that it's null :(
                }
                else Log.e(LOG_TAG, "Contact not added.");
            }   catch (RemoteException e) { 
                // error
                Log.e(LOG_TAG, "Error (1) adding contact.");
                newContactUri = null;
            }   catch (OperationApplicationException e) {
                // error
                Log.e(LOG_TAG, "Error (2) adding contact.");
                newContactUri = null;
            }  
            Log.d(LOG_TAG, "Contact added to system contacts.");

            if (newContactUri == null) {
                Log.e(LOG_TAG, "Error creating contact");
            }
        }

I also tried to use Insert instead of update but with Insert my application crashed when I tried to retrieve "newContactUri = res[0].uri;" I have searched for similar solutions but nothing worked for me :/


回答1:


Topic linked from MAYUR BHOLA helped, thx. I'm posting working version of my problem, maybe someone will need this.

public static final String             MIMETYPE_EMPLOYEE   = "vnd.android.cursor.item/employee";
    private void updateEmployee(String id, String value){
    try {
        ContentValues values = new ContentValues();
        values.put(Data.DATA1, value);
        int mod = act.getContentResolver().update(
                Data.CONTENT_URI,
                values,
                Data.RAW_CONTACT_ID + "=" + id + " AND "
                        + Data.MIMETYPE + "= '"
                        + MIMETYPE_EMPLOYEE + "'", null);

        if (mod == 0) {
            values.put(Data.RAW_CONTACT_ID, id);
            values.put(Data.MIMETYPE, MIMETYPE_EMPLOYEE);
            act.getContentResolver().insert(Data.CONTENT_URI, values);
            Log.v(LOG_TAG, "data inserted");
        } else {
            Log.v(LOG_TAG, "data updated");
        }
    } catch (Exception e) {
        Log.v(LOG_TAG, "failed");
    }
}


来源:https://stackoverflow.com/questions/9053352/adding-custom-data-to-contacts-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!