0

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

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

建议/帮助将不胜感激

4

1 回答 1

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();
                }
            } 
于 2013-09-19T17:11:20.530 回答