问题
I tried following codes for adding new contact which is not working in my device.. but same code is working fine with Emulator.. I am using Samsung Galaxy fit GT-s5670.
CODE STYLE - 1
ContentValues values = new ContentValues();
values.put(People.NAME,"test contact");
Uri uri = getContentResolver().insert(People.CONTENT_URI, values);
Uri phoneUri = null;
Uri emailUri = null;
phoneUri = Uri.withAppendedPath(uri, People.Phones.CONTENT_DIRECTORY);
values.clear();
values.put(People.Phones.TYPE, People.Phones.TYPE_MOBILE);
values.put(People.Phones.NUMBER,"0123456789");
getContentResolver().insert(phoneUri, values);
emailUri = Uri.withAppendedPath(uri, People.ContactMethods.CONTENT_DIRECTORY);
values.clear();
values.put(People.ContactMethods.KIND, Contacts.KIND_EMAIL);
values.put(People.ContactMethods.DATA,"test@test.com");
values.put(People.ContactMethods.TYPE, People.ContactMethods.TYPE_HOME);
getContentResolver().insert(emailUri, values);
CODE STYLE - 2
Intent intent = new Intent(Contacts.Intents.Insert.ACTION, Contacts.People.CONTENT_URI);
intent.putExtra(ContactsContract.Intents.Insert.NAME,"test contact");
intent.putExtra(ContactsContract.Intents.Insert.PHONE,"0123456789");
intent.putExtra(ContactsContract.Intents.Insert.EMAIL,"test@test.com");
startActivity(intent);
I tried above both methods but i can't add in my device. Please help me to solve..!
Thank you.
回答1:
try this link
http://androiddevelopement.blogspot.com/2011/07/insert-update-delete-view-contacts-in.html or use this code.
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();
ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
.withValue(RawContacts.ACCOUNT_TYPE, null)
.withValue(RawContacts.ACCOUNT_NAME,null )
.build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(Data.MIMETYPE,Phone.CONTENT_ITEM_TYPE)
.withValue(Phone.NUMBER, "9X-XXXXXXXXX")
.build());
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(Data.MIMETYPE,
StructuredName.CONTENT_ITEM_TYPE)
.withValue(StructuredName.DISPLAY_NAME, "Mike Sullivan")
.build());
ContentProviderResult[] res = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
回答2:
check these links:
Inserting contacts in Android 2.2
How to add new contacts in android
Android - New Data record is added to the wrong contact
来源:https://stackoverflow.com/questions/7280152/unable-to-add-contact-in-my-device