无法在我的设备中添加联系人

时间:2011-09-02 06:59:18

标签: android-contacts android

我尝试使用以下代码添加在我的设备中无效的新联系人。 但相同的代码与模拟器工作正常.. 我正在使用三星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);

我尝试了两种方法,但我无法添加到我的设备中。请帮我解决..!

谢谢。

2 个答案:

答案 0 :(得分:2)

试试此链接

http://androiddevelopement.blogspot.com/2011/07/insert-update-delete-view-contacts-in.html 或使用此代码。

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);

答案 1 :(得分:0)