Naive Algorist
Naive Algorist

Reputation: 71

How to Add Contacts to Contacts Table

I am trying to insert a contact through my application but i am not able to figure out what should be the value of accountType and accountName as below.

ContentValues values = new ContentValues();
values.put(RawContacts.ACCOUNT_TYPE, accountType);
values.put(RawContacts.ACCOUNT_NAME, accountName);
Uri rawContactUri = getContentResolver().insert(RawContacts.CONTENT_URI, values);
long rawContactId = ContentUris.parseId(rawContactUri);


values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
values.put(StructuredName.DISPLAY_NAME, "Mike Sullivan");
values.put(ContactsContract.CommonDataKinds.Phone.NUMBER,"1-800-111-411");
getContentResolver().insert(Data.CONTENT_URI, values);

Also when i try to execute this code with the following changes in the accountType and accountName, i am unable to see it in the Contacts.

values.put(RawContacts.ACCOUNT_TYPE, "acc_type");
values.put(RawContacts.ACCOUNT_NAME, "acc_name");

But it seems that some values get inserted as when i search for "Mike Sullivan" i get the contact but without the Phone Number. Please Help

Upvotes: 0

Views: 1471

Answers (2)

Shankar Agarwal
Shankar Agarwal

Reputation: 34775

below is the code to add contact database and also it return whether the contact was added or not::::

//to save contact in Database
public boolean SaveContact(Activity _activity,String name,String number) {
    String MIMETYPE_RADUTOKEN   = "vnd.android.cursor.item/radutoken";
    String  szname = name,szMobile = number;
    //Create a new contact entry!
    String szToken = String.format("RADU_TOKEN_%d", System.currentTimeMillis());
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    int rawContactInsertIndex = ops.size();

    ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI).withValue(RawContacts.ACCOUNTTYPE, null).withValue(RawContacts.ACCOUNT_NAME, null).build());

    //INSERT NAME
    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,rawContactInsertIndex).withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE).withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, szname).build());

    //INSERT PINLESSMAX MOBILE NUMBER
    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,   rawContactInsertIndex).withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE).withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, szMobile).withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM).withValue(ContactsContract.Data.DATA3, "PinLessMax").build());

    // SAVE CONTACT IN BCR Structure
    Uri newContactUri = null;
    //PUSH EVERYTHING TO CONTACTS
    try{
       ContentProviderResult[] res = _activity.getContentResolver().applyBatch(ContactsContract.AUTHORITY,ops);
      if (res!=null && res[0]!=null) {
         newContactUri = res[0].uri;    
     }
    }catch (RemoteException e) { 
        // error
        newContactUri = null;
    }  catch (OperationApplicationException e)   {
        // error
         newContactUri = null;
    }  
    if (newContactUri == null) {
        return false;
     }
    boolean foundToken = false;

  // IDENTIFY Contact based on name and token
    String szLookupKey = "";
    Uri lkup = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_FILTER_URI, szname);
    ContentResolver cr = _activity.getContentResolver();
    Cursor idCursor = _activity.getContentResolver().query(lkup, null, null, null, null);
    // get all the names 
    while (idCursor.moveToNext()) {
        String szId = idCursor.getString(idCursor.getColumnIndex(ContactsContract.Contacts._ID));
        String szName = idCursor.getString(idCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        szLookupKey = idCursor.getString(idCursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
       // for this contact ID, search the custom field
       String tokenWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; 
      String[] tokenWhereParams = new String[]{szId, MIMETYPE_RADUTOKEN}; 
      Cursor tokenCur = cr.query(ContactsContract.Data.CONTENT_URI, null, tokenWhere, tokenWhereParams, null); 
      while (tokenCur.moveToNext()) {
         String token = tokenCur.getString(tokenCur.getColumnIndex(ContactsContract.Data.DATA1));
        // CHECK THE TOKEN!
        if (szToken.compareTo(token) == 0) {
          tokenCur.close();
          foundToken = true;
          break;
        }   
       } 
      tokenCur.close();
      if (foundToken) break;
  }
  idCursor.close();
  return true;
}//SaveContact()

Upvotes: 2

San
San

Reputation: 5697

You can give null values for account type and account name if you do not want to create the contact under a specific account

values.put(RawContacts.ACCOUNT_TYPE, null);
values.put(RawContacts.ACCOUNT_NAME, null);

In most of the devices it creates as a default phone contact.

If you want to know all the account types available in the device, You can use the following code

Account[] accountList = AccountManager.get(this).getAccounts();
for(int i = 0 ; i < accountList.length ; i++) {
  System.out.println(accountList[i].type);
}

Note: Different OEMs uses different name for account type.

Upvotes: 0

Related Questions