Adding a contact – no error, but the contacts don’t get added

I need to add a list of contacts, I’m using this code:

private void addContact(String name, String phone, Integer id) {
    ContentValues values = new ContentValues();
    values.put(ContactsContract.Data.RAW_CONTACT_ID, id);
    values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
    values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, phone);
    values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM);
    values.put(ContactsContract.CommonDataKinds.Phone.LABEL, name);
    Uri dataUri = getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);
  }


  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    for (int i = 0; i < 10; i++) {
      addContact("name_" + i, "+112233445566" + i, i);
    }
  }

#Manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="android.me123.com.me456" >

    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />


#Gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 20
    buildToolsVersion "20"

    defaultConfig {
        applicationId "android.me123.com.me456"
        minSdkVersion 15
        targetSdkVersion 20
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

And just nothing happens, no error either. What’s up with it?

And how do I set the name of the new contact? Phone.LABEL isn’t what I’m looking for.

Answer

Instead of Phone.LABEL use ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME here is the complete method to add contact

public void addtocontact(String DisplayName, String MobileNumber) {
        // TODO Auto-generated method stub

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

        // ------------------------------------------------------
        // 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());
        }
        getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

    }

Leave a Reply

Your email address will not be published. Required fields are marked *