我正在开发检索所有 android 联系人并且还可以删除和更新联系人的应用程序。一切正常的问题是当联系人没有电子邮件地址时,我无法使用我的应用程序更新它(无法从我的更新中添加电子邮件地址联系页面)。我在下面写了用过的代码。提前致谢。
private void updateEmaill(String id, String email) {
ContentResolver cr = getContentResolver();
String where = ContactsContract.Data.RAW_CONTACT_ID + " = ? AND "
+ ContactsContract.Data.MIMETYPE + " = ?";
String[] params = new String[] { id,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE };
Cursor phoneCur = managedQuery(ContactsContract.Data.CONTENT_URI, null,
where, params, null);
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
if ((null == phoneCur)) {
System.out.println("we have got null value from the cursor");
} else {
System.out.println("phone cursor count" + phoneCur.getCount());
ops.add(ContentProviderOperation
.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(where, params)
.withValue(ContactsContract.CommonDataKinds.Email.ADDRESS,
email).build());
}
phoneCur.close();
try {
cr.applyBatch(ContactsContract.AUTHORITY, ops);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OperationApplicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}