无法在电话簿中添加联系人

时间:2013-09-19 16:33:44

标签: android android-contentprovider android-contacts

我正在尝试添加名称&电话号码进入电话簿。它始终显示“已插入”,但它从未出现在电话簿中。

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

                int index = ops.size();

                ops.add(ContentProviderOperation
                        .newInsert(ContactsContract.Data.CONTENT_URI)
                        .withValueBackReference(
                                ContactsContract.Data.RAW_CONTACT_ID, index)
                        .withValue(
                                ContactsContract.Data.MIMETYPE,
                                ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                        .withValue(
                                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                                mDetail.get(position).getName())
                        .withValue(
                                ContactsContract.CommonDataKinds.Phone.NUMBER,
                                mDetail.get(position).getPhone())
                        .withValue(
                                ContactsContract.CommonDataKinds.Phone.TYPE,
                                ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
                        .build());

                try {
                    getContentResolver().applyBatch(
                            ContactsContract.AUTHORITY, ops);

                    Toast.makeText(Detail_Activity.this, "Inserted..",
                            Toast.LENGTH_SHORT).show();

                } catch (Exception e) {

                    Toast.makeText(Detail_Activity.this, e.getMessage(),
                            Toast.LENGTH_SHORT).show();
                }

建议/帮助将受到高度赞赏

1 个答案:

答案 0 :(得分:0)

我有解决方案。它有效!!

        String name = mDetail.get(position).getName();
        String phone = mDetail.get(position).getPhone();



                ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

                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(Data.CONTENT_URI)
                        .withValueBackReference(Data.RAW_CONTACT_ID, 0)
                        .withValue(Data.MIMETYPE,
                                StructuredName.CONTENT_ITEM_TYPE)
                        .withValue(StructuredName.DISPLAY_NAME, name)
                        .build());

                ops.add(ContentProviderOperation
                        .newInsert(Data.CONTENT_URI)
                        .withValueBackReference(Data.RAW_CONTACT_ID, 0)
                        .withValue(Data.MIMETYPE,
                                Phone.CONTENT_ITEM_TYPE)
                        .withValue(Phone.NUMBER, phone)
                        .withValue(Phone.TYPE, Phone.TYPE_MOBILE)
                        .build());

                try {

                    getContentResolver().applyBatch(
                            ContactsContract.AUTHORITY, ops);

                    Toast.makeText(Detail_Activity.this,
                            "Contact Saved..", Toast.LENGTH_SHORT)
                            .show();
                } catch (Exception e) {

                    Toast.makeText(Detail_Activity.this,
                            e.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }