Android 编辑联系人
/** * @brief 显示联系人编辑界面 * */ public void editContact(String contactId,Context context) { if (context == null || contactId == null) { return ; } Uri uri = getContactUri(contactId,context); if (uri != null) { Intent editIntent = new Intent(Intent.ACTION_EDIT); editIntent.setDataAndType(uri,ContactsContract.Contacts.CONTENT_ITEM_TYPE); // editIntent.putExtra("finishActivityOnSaveCompleted", true); context.startActivity(editIntent); } else { } } /** * 修改联系人信息 * */ public void updateContact(Contact contactOld, Contact contactNew) { Log.w(TAG, "**update start**"); String id = getContactID(contactOld.getName()); if(id.equals("0")) { Log.d(TAG, contactOld.getName()+" not exist."); }/* else if(contactNew.getName().trim().equals("")){ Log.d(TAG, "contact name is empty. exit."); } else if(!getContactID(contactNew.getName()).equals("0")){ Log.d(TAG, "new contact name already exist. exit."); }*/ else { ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); //update name // ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI) // .withSelection(COLUMN_CONTACT_ID + "=? AND " + COLUMN_MIMETYPE + "=?", // new String[]{id, MIMETYPE_STRING_NAME}) // .withValue(COLUMN_NAME, contactNew.getName()) // .build()); Log.d(TAG, "update name: " + contactNew.getName()); // photo ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI) .withSelection(COLUMN_CONTACT_ID + "=? AND " + COLUMN_MIMETYPE + "=?", new String[]{id, MIMETYPE_STRING_PHOTO}) .withValue(COLUMN_PHOTO, contactNew.getPhotoData()) .build()); // web ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI) .withSelection(COLUMN_CONTACT_ID + "=? AND " + COLUMN_MIMETYPE + "=?", new String[]{id, MIMETYPE_STRING_WEB}) .withValue(COLUMN_WEB,contactNew.getHomePage()) .withValue(COLUMN_WEB_TYPE, contactNew.getHomePage()) .build()); // postal ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI) .withSelection(COLUMN_CONTACT_ID + "=? AND " + COLUMN_MIMETYPE + "=?", new String[]{id, MIMETYPE_STRING_ADDRESS}) .withValue(COLUMN_POSTAL,contactNew.getAddress()) .withValue(COLUMN_POSTAL_TYPE, contactNew.getAddressType()) .build()); //update number if(!contactNew.getNumber().trim().equals("")) { ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI) .withSelection(COLUMN_CONTACT_ID + "=? AND " + COLUMN_MIMETYPE + "=? AND " + COLUMN_NUMBER_TYPE + "=?", new String[]{id, MIMETYPE_STRING_PHONE,contactNew.getNumberType()}) .withValue(COLUMN_NUMBER, contactNew.getNumber()) .withValue(COLUMN_NUMBER_TYPE, contactNew.getNumberType()) .build()); Log.d(TAG, "update number: " + contactNew.getNumber()); } // fax if(!contactNew.getNumber().trim().equals("")) { ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI) .withSelection(COLUMN_CONTACT_ID + "=? AND " + COLUMN_MIMETYPE + "=? AND " + COLUMN_NUMBER_TYPE + "=?", new String[]{id, MIMETYPE_STRING_PHONE,contactNew.getFaxType()}) .withValue(COLUMN_NUMBER, contactNew.getFax()) .withValue(COLUMN_NUMBER_TYPE, contactNew.getFaxType()) .build()); Log.d(TAG, "update fax: " + contactNew.getNumber()); } //update email if mail if(!contactNew.getEmail().trim().equals("")) { ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI) .withSelection(COLUMN_CONTACT_ID + "=? AND " + COLUMN_MIMETYPE + "=?", new String[]{id, MIMETYPE_STRING_EMAIL}) .withValue(COLUMN_EMAIL, contactNew.getEmail()) .withValue(COLUMN_EMAIL_TYPE, contactNew.getEmailType()) .build()); Log.d(TAG, "update email: " + contactNew.getEmail()); } try { contentResolver.applyBatch(ContactsContract.AUTHORITY, ops); Log.d(TAG, "update success"); } catch (Exception e) { Log.d(TAG, "update failed"); Log.e(TAG, e.getMessage()); } } Log.w(TAG, "**update end**"); }