Android : open an intent of modifying contact by Content Provider Operation

on Saturday, October 11, 2014


I want to open an intent for modifying contact by the content provider operation. my previous code that done that was this:



intent1.setType(ContactsContract.RawContacts.CONTENT_TYPE);

intent1.putExtra(Contacts.Intents.Insert.PHONE, user_phoneNumber).
putExtra(Contacts.Intents.Insert.PHONE_TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE).
putExtra(Contacts.Intents.Insert.NAME, user_userName).
putExtra(Contacts.Intents.Insert.EMAIL, user_email2);

startActivity(intent1);


this opend me the intent, but I wanted to add an website to the modify so i have done it with content provider opraation.



ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
.build());

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, "omri") // Name of the person


// .withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, szLastname) // Name of the person // .withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, szFirstname) // Name of the person .build());



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, "number") // Number of the person
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
.build()); //


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.DATA, "omrisavra@gmail.com")
.withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK)
.build());

ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Website.URL, "omm.com") //
.withValue(ContactsContract.CommonDataKinds.Website.TYPE, ContactsContract.CommonDataKinds.Website.TYPE_WORK)
.build());


Uri newContactUri = null;
try
{
ContentProviderResult[] res = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
if (res!=null && res[0]!=null) {
newContactUri = res[0].uri;
//02-20 22:21:09 URI added contact:content://com.android.contacts/raw_contacts/612
Log.d(LOG_TAG, "URI added contact:"+ newContactUri);
}
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");

}


In this way the the contact is saved without opening any intent of modify contact. how to open an intent of modify contact with conetent provider operation?


0 comments:

Post a Comment