Warm tip: This article is reproduced from serverfault.com, please click

Add to contacts not performed immediately after permission is granted

发布于 2021-02-25 14:22:25

In my Android application (written in Java) I have a feature to add a contact to the user's phone using scanned information. The feature works perfectly once the user grants the application permission to edit the device's contacts. However, after granting permission the user needs to press the 'Add to Contacts' button again since the action was not performed, to begin with. It's as if the original command was thrown out in lew of asking the user for permission to do that action. Is there any way to make it so that the contact is added immediately after permission is granted? A user using the application for the first time would not know that the first time they add a contact they need to press the button a second time after granting permission.

Here is the code for the 'Add to Contacts' method:

private void addToContact(View v) {
    //if the permission if off
    Ask.on(this)
            .forPermissions(Manifest.permission.WRITE_CONTACTS)
            .go();

    int phoneType = 1;
    int emailType = 1;

    // Prepare contact creation request
    //
    // Note: We use RawContacts because this data must be associated with a
    // particular account.
    // The system will aggregate this with any other data for this contact
    // and create a coresponding entry in the ContactsContract.Contacts provider for us.
    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(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
            .withValue(
                    ContactsContract.Data.MIMETYPE,
                    ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
            .withValue(
                    ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
                    mContactName).build());
    ops.add(ContentProviderOperation
            .newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
            .withValue(
                    ContactsContract.Data.MIMETYPE,
                    ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,
                    mContactPhoneNumber)
            .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
                    phoneType).build());
    ops.add(ContentProviderOperation
            .newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
            .withValue(
                    ContactsContract.Data.MIMETYPE,
                    ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.Email.DATA,
                    mContactEmail)
            .withValue(ContactsContract.CommonDataKinds.Email.TYPE,
                    emailType).build());
    ops.add(ContentProviderOperation
            .newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
            .withValue(
                    ContactsContract.Data.MIMETYPE,
                    ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
            .withValue(
                    ContactsContract.CommonDataKinds.Organization.COMPANY,
                    mContactOrg)
            .withValue(ContactsContract.CommonDataKinds.Organization.TITLE,
                    mContactTitle)
            .withValue(ContactsContract.CommonDataKinds.Organization.TYPE,
                    1).build());
    ops.add(ContentProviderOperation
            .newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
            .withValue(
                    ContactsContract.Data.MIMETYPE,
                    ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
            .withValue(
                    ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS,
                    mContactAddress).build());

    // Ask the Contact provider to create a new contact
    Log.i(TAG, "Selected account: " + ACCOUNT_TYPE + " (" + ACCOUNT_NAME
            + ")");
    Log.i(TAG, "Creating contact: " + mContactName);
    int duration = Toast.LENGTH_SHORT;
    Context ctx = getApplicationContext();
    try {
        getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
        Toast toast = Toast.makeText(ctx, "contact created  successfully",
                duration);
        toast.show();

    } catch (Exception e) {
        // Display warning

        Toast toast = Toast.makeText(ctx, "contact creation failed",
                duration);
        toast.show();

        // Log exception
        Log.e(TAG, "Exception encountered while inserting contact: " + e);
    }
}

Here is where the method is called:

mAddToContactButton.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            addToContact(v);
        }
    });
Questioner
Sarah Cohen
Viewed
0
Sarah Cohen 2021-02-26 04:46:59

I used the answer to this question to make it work:Android contacts permission granted without asking at runtime @Marmor was correct in what the issue was, however, the Ask function was not working. With Android 23 and higher more is needed.