[英]Android programming updating contacts
I am trying to update a phone number for a specific record in a phone book, this is the code I have, but for some reason the field is not updated and I cant find the problem: 我正在尝试为电话簿中的特定记录更新电话号码,这是我的代码,但是由于某些原因,该字段未更新,因此我找不到问题:
PS: I put the permission in manifest file for writing into contacts. PS:我将权限放入清单文件中以写入联系人。
final ContentResolver cr = getContentResolver();
final Cursor cur = cr.query(Phone.CONTENT_URI, null, null, null, null);
//***********************
change.setOnClickListener(new OnClickListener ()
{
public void onClick(View v)
{
while(cur.moveToNext())
{
String id = cur.getString(cur.getColumnIndexOrThrow(Phone.DISPLAY_NAME ));
if(id.equals("Jack"))
{
cur.getColumnIndexOrThrow(Phone.DISPLAY_NAME );
String lookupKey = cur.getString(cur.getColumnIndex(Phone.LOOKUP_KEY));
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
ContentValues value = new ContentValues();
value.clear();
value.put(Phone.NUMBER, "885544");
cr.update(uri,value, null, null);
}
}
}
});
This should do the job: 这应该做的工作:
void updatePhoneNumber(String contactid, String phoneNo) {
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
builder = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);
builder.withSelection(ContactsContract.Data.CONTACT_ID + "=?" + " AND " +
ContactsContract.Data.MIMETYPE + "=?"+ " AND " +
ContactsContract.CommonDataKinds.Organization.TYPE + "=?",
new String[]{String.valueOf(contactid), ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_HOME)});
builder.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phoneNo);
ops.add(builder.build());
try {
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
// handle error
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.