添加自定义数据在Android通讯录 [英] Adding custom data to contacts in Android
问题描述
我要自定义字段添加到联系人会告诉我,如果接触被标记在我aplication与否。
首先我要特别强调将设置我的自定义数据与给定的ID联系功能,但code,我尝试使用,不能正常工作。
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");
}
}
我也试过用插入,而不是更新但插入我的应用程序崩溃时,我试图找回newContactUri = RES [0] .uri;
我已经寻找类似的解决方案,但没有工作对我来说:/
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 :/
推荐答案
主题从MAYUR博拉联的帮助下,THX。
我张贴的工作我的版本问题,也许有人会需要这个。
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");
}
}
这篇关于添加自定义数据在Android通讯录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!