添加新联系失败

时间:2016-06-26 04:35:07

标签: android android-contentprovider android-permissions android-contentresolver

我必须创建一个方法,将联系人添加到手机的联系人列表中。根据许多不同帖子的建议,我想出了这个方案:

           private void addContact() {

                mContentValues = new ContentValues();
                mContentResolver = getContentResolver();
                mUri = Uri.parse("content://com.android.contacts/raw_contacts");
                long id = ContentUris.parseId(mContentResolver.insert(mUri, mContentValues));
                mUri = Uri.parse("content://com.android.contacts/data");
                mContentValues.put(ContactsContract.Data.RAW_CONTACT_ID, id);
                mContentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);

                mContentValues.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, mDisplayedName);
                mContentValues.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, person.getFirstName());
                mContentValues.put(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, person.getLastName());

                mContentValues.put(ContactsContract.CommonDataKinds.Email.ADDRESS, person.getEmail());

                mContentValues.put(ContactsContract.CommonDataKinds.Phone.NUMBER, mFinalPhoneNumber);

                mContentValues.put(ContactsContract.CommonDataKinds.StructuredPostal.STREET, person.getStreetAddress());
                mContentValues.put(ContactsContract.CommonDataKinds.StructuredPostal.CITY, person.getCity());
                mContentValues.put(ContactsContract.CommonDataKinds.StructuredPostal.REGION, person.getState());
                mContentValues.put(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, person.getZipCode());

mContentValues.put(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_HOME);
                    mContentValues.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_HOME);

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.WRITE_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
                    requestPermissions(new String[]{Manifest.permission.WRITE_CONTACTS}, PERMISSIONS_REQUEST_WRITE_CONTACTS);
                } else {
                    addContact();
                }
            }

            @Override
            public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {

                switch (requestCode) {

                    case PERMISSIONS_REQUEST_WRITE_CONTACTS:
                        if (grantResults.length > 0 &&grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                            addContact();
                        } else {
                            Toast.makeText(this, "Insufficient permissions", Toast.LENGTH_SHORT).show();
                        }
                }
            }

            private void addContact() {

                if (mContentValues == null)
                    return;

                Uri uri = mContentResolver.insert(mUri, mContentValues);

                if (uri == null)
                    Log.d(TAG, "didnt work");
            }

这将运行完成,没有错误,警告或控制台输出。但是从未添加过该联系人。我认为最可能发生错误的地方是尽早分配原始联系人ID,但这只是猜测。

谁能看到我做错了什么?谢谢!

1 个答案:

答案 0 :(得分:0)

尝试改变如下:

Uri uri = getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, mContentValues);