Insert a new contact intent

后端 未结 9 998
陌清茗
陌清茗 2020-11-30 23:02

For one of my apps, I need the user to select one of his existing contacts or to create a new one. Picking one is clearly easy to do with the following code:



        
9条回答
  •  -上瘾入骨i
    2020-11-30 23:56

    You can choose whether you want to add the contact automatically, or open the add contact activity with pre-filled data:

    /**
     * Open the add-contact screen with pre-filled info
     * 
     * @param context
     *            Activity context
     * @param person
     *            {@link Person} to add to contacts list
     */
    public static void addAsContactConfirmed(final Context context, final Person person) {
    
        Intent intent = new Intent(Intent.ACTION_INSERT);
        intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
    
        intent.putExtra(ContactsContract.Intents.Insert.NAME, person.name);
        intent.putExtra(ContactsContract.Intents.Insert.PHONE, person.mobile);
        intent.putExtra(ContactsContract.Intents.Insert.EMAIL, person.email);
    
        context.startActivity(intent);
    
    }
    
    /**
     * Automatically add a contact into someone's contacts list
     * 
     * @param context
     *            Activity context
     * @param person
     *            {@link Person} to add to contacts list
     */
    public static void addAsContactAutomatic(final Context context, final Person person) {
        String displayName = person.name;
        String mobileNumber = person.mobile;
        String email = person.email;
    
        ArrayList ops = new ArrayList();
    
        ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
                .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
                .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null).build());
    
        // Names
        if (displayName != null) {
            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,
                            displayName).build());
        }
    
        // Mobile Number
        if (mobileNumber != null) {
            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, mobileNumber)
                    .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
                            ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE).build());
        }
    
        // Email
        if (email != null) {
            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, email)
                    .withValue(ContactsContract.CommonDataKinds.Email.TYPE,
                            ContactsContract.CommonDataKinds.Email.TYPE_WORK).build());
        }
    
        // Asking the Contact provider to create a new contact
        try {
            context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        Toast.makeText(context, "Contact " + displayName + " added.", Toast.LENGTH_SHORT)
                .show();
    }
    

提交回复
热议问题