如何在 Android 中以编程方式更新 'profile' 联系人?
How to update 'profile' contact programmatically in Android?
我想在我的应用程序中编辑设备主要用户的配置文件。所以我使用 ContentProviderOperation
和 newUpdate
方法来收集更改,然后使用 ContentResolver
方法提交它们:applyBatch
。这是我到目前为止编写的代码:
public ContentProviderResult[] updateProfile(Contact userProfile) {
try {
ArrayList<ContentProviderOperation> ops = new ArrayList<>();
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, Integer.valueOf(userProfile.id))
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, edtName.getText().toString().trim())
.build());
return getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
我收到任何 运行 时间异常,但未提交任何更改!
我错过了什么吗?
我已经弄明白了,问题是我不知道如何检索用户配置文件 RAW_CONTACT_ID
以在 ContactsContract.Data
table 上投影。
我的快速解决方法是删除用户配置文件记录,然后创建一个新的。这是我目前使用的代码:
public ContentProviderResult[] createOrUpdateUserProfile() {
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();
try {
ops.add(ContentProviderOperation.
newDelete(ContactsContract.Profile.CONTENT_RAW_CONTACTS_URI)
.build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
ops.clear();
//Create User Profile
ops.add(ContentProviderOperation.newInsert(ContactsContract.Profile.CONTENT_RAW_CONTACTS_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
.build());
//Create User Name
if (!(edtName.getText().toString().matches(""))) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, edtName.getText().toString())
.build());
}
if (!(edtHomePhone.getText().toString().matches(""))) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, edtHomePhone.getText().toString())
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 1)
.build());
}
if (!(edtMobilePhone.getText().toString().matches(""))) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, edtMobilePhone.getText().toString())
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 2)
.build());
}
if (!(edtWorkPhone.getText().toString().matches(""))) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, edtWorkPhone.getText().toString())
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 3)
.build());
}
if (!(edtHomeEmail.getText().toString().matches(""))) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.ADDRESS, edtHomeEmail.getText().toString())
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 1) // contact.numbers.get(0).type
.build());
}
if (!(edtWorkEmail.getText().toString().matches(""))) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.ADDRESS, edtWorkEmail.getText().toString())
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 2) // contact.numbers.get(0).type
.build());
}
/*String completeAddress = edtStreet.getText().toString() + edtCity.getText().toString() + edtState.getText().toString()
+ edtCountry.getText().toString();*/
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET, edtStreet.getText().toString())
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.CITY, edtCity.getText().toString())
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.REGION, edtState.getText().toString())
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, edtCountry.getText().toString())
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, edtZip.getText().toString())
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, 1)
.build());
if (!(edtSkype.getText().toString().matches(""))) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Im.DATA, edtSkype.getText().toString())
.withValue(ContactsContract.CommonDataKinds.Im.PROTOCOL, 3)
.build());
}
ContentProviderResult[] res = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
return res;
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OperationApplicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
你的代码有点错误。
public ContentProviderResult[] updateProfile(Contact userProfile) {
try {
ArrayList<ContentProviderOperation> ops = new ArrayList<>();
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, Integer.valueOf(userProfile.id)) // Error is here
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, edtName.getText().toString().trim())
.build());
return getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
用于此行:
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, Integer.valueOf(userProfile.id))
这个
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, getRawContactId(id))
public static String getRawContactId(String contactId)
{
String res = "";
Uri uri = ContactsContract.RawContacts.CONTENT_URI;
String[] projection = new String[]{ContactsContract.RawContacts._ID};
String selection = ContactsContract.RawContacts.CONTACT_ID + " = ?";
String[] selectionArgs = new String[]{ contactId };
Cursor c = ContentManager.getContentResolver().query(uri, projection, selection, selectionArgs, null);
if(c != null && c.moveToFirst())
{
res = c.getString(c.getColumnIndex(ContactsContract.RawContacts._ID));
c.close();
}
return res;
}
我想在我的应用程序中编辑设备主要用户的配置文件。所以我使用 ContentProviderOperation
和 newUpdate
方法来收集更改,然后使用 ContentResolver
方法提交它们:applyBatch
。这是我到目前为止编写的代码:
public ContentProviderResult[] updateProfile(Contact userProfile) {
try {
ArrayList<ContentProviderOperation> ops = new ArrayList<>();
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, Integer.valueOf(userProfile.id))
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, edtName.getText().toString().trim())
.build());
return getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
我收到任何 运行 时间异常,但未提交任何更改! 我错过了什么吗?
我已经弄明白了,问题是我不知道如何检索用户配置文件 RAW_CONTACT_ID
以在 ContactsContract.Data
table 上投影。
我的快速解决方法是删除用户配置文件记录,然后创建一个新的。这是我目前使用的代码:
public ContentProviderResult[] createOrUpdateUserProfile() {
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();
try {
ops.add(ContentProviderOperation.
newDelete(ContactsContract.Profile.CONTENT_RAW_CONTACTS_URI)
.build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
ops.clear();
//Create User Profile
ops.add(ContentProviderOperation.newInsert(ContactsContract.Profile.CONTENT_RAW_CONTACTS_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
.build());
//Create User Name
if (!(edtName.getText().toString().matches(""))) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, edtName.getText().toString())
.build());
}
if (!(edtHomePhone.getText().toString().matches(""))) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, edtHomePhone.getText().toString())
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 1)
.build());
}
if (!(edtMobilePhone.getText().toString().matches(""))) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, edtMobilePhone.getText().toString())
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 2)
.build());
}
if (!(edtWorkPhone.getText().toString().matches(""))) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, edtWorkPhone.getText().toString())
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 3)
.build());
}
if (!(edtHomeEmail.getText().toString().matches(""))) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.ADDRESS, edtHomeEmail.getText().toString())
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 1) // contact.numbers.get(0).type
.build());
}
if (!(edtWorkEmail.getText().toString().matches(""))) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.ADDRESS, edtWorkEmail.getText().toString())
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 2) // contact.numbers.get(0).type
.build());
}
/*String completeAddress = edtStreet.getText().toString() + edtCity.getText().toString() + edtState.getText().toString()
+ edtCountry.getText().toString();*/
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET, edtStreet.getText().toString())
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.CITY, edtCity.getText().toString())
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.REGION, edtState.getText().toString())
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, edtCountry.getText().toString())
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, edtZip.getText().toString())
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, 1)
.build());
if (!(edtSkype.getText().toString().matches(""))) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Im.DATA, edtSkype.getText().toString())
.withValue(ContactsContract.CommonDataKinds.Im.PROTOCOL, 3)
.build());
}
ContentProviderResult[] res = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
return res;
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OperationApplicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
你的代码有点错误。
public ContentProviderResult[] updateProfile(Contact userProfile) {
try {
ArrayList<ContentProviderOperation> ops = new ArrayList<>();
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, Integer.valueOf(userProfile.id)) // Error is here
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, edtName.getText().toString().trim())
.build());
return getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
用于此行:
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, Integer.valueOf(userProfile.id))
这个
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, getRawContactId(id))
public static String getRawContactId(String contactId)
{
String res = "";
Uri uri = ContactsContract.RawContacts.CONTENT_URI;
String[] projection = new String[]{ContactsContract.RawContacts._ID};
String selection = ContactsContract.RawContacts.CONTACT_ID + " = ?";
String[] selectionArgs = new String[]{ contactId };
Cursor c = ContentManager.getContentResolver().query(uri, projection, selection, selectionArgs, null);
if(c != null && c.moveToFirst())
{
res = c.getString(c.getColumnIndex(ContactsContract.RawContacts._ID));
c.close();
}
return res;
}