Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add new contacts in android

Tags:

android

I have written following code to add new contact in android phone book, it is working but when i open contact menu, i cannot see the new contact added. Can anyone help me to find out what's wrong here?

import android.app.Activity; import android.os.Bundle;  import android.content.ContentResolver; import android.content.ContentValues; import android.provider.ContactsContract;  import android.widget.TextView; import android.widget.Toast;  public class AddContacts extends Activity {     /** Called when the activity is first created. */     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);         try {             ContentResolver cr = this.getContentResolver();             ContentValues cv = new ContentValues();             cv.put(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, "New Name");             cv.put(ContactsContract.CommonDataKinds.Phone.NUMBER, "1234567890");             cv.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);             cr.insert(ContactsContract.RawContacts.CONTENT_URI, cv);              Toast.makeText(this, "Contact added", Toast.LENGTH_LONG).show();         } catch(Exception e) {             TextView tv = new TextView(this);             tv.setText(e.toString());             setContentView(tv);         }     } } 
like image 389
Aditya Mehta Avatar asked Jan 20 '11 06:01

Aditya Mehta


People also ask

How do I create a new contact?

Open the Phone app. Go to the Call History tab and tap the phone number you wish to add to your contacts. Tap Create New Contact. Tap in each labeled area to type in the contact's details.

How do I add contacts quickly?

If you're on Android and you add contacts a lot, you can take it an extra step and long-press the "Add Contact" button in the popup, then drag that out and drop it on your home screen so the feature is always one tap away.


2 Answers

Here I am posting a piece of code that i use to add a new contact. It works fine for me. I hope it will help you.

 String DisplayName = "XYZ";  String MobileNumber = "123456";  String HomeNumber = "1111";  String WorkNumber = "2222";  String emailID = "[email protected]";  String company = "bad";  String jobTitle = "abcd";   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());  }   //------------------------------------------------------ Home Numbers  if (HomeNumber != 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, HomeNumber)          .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,      ContactsContract.CommonDataKinds.Phone.TYPE_HOME)          .build());  }   //------------------------------------------------------ Work Numbers  if (WorkNumber != 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, WorkNumber)          .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,      ContactsContract.CommonDataKinds.Phone.TYPE_WORK)          .build());  }   //------------------------------------------------------ Email  if (emailID != 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, emailID)          .withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK)          .build());  }   //------------------------------------------------------ Organization  if (!company.equals("") && !jobTitle.equals("")) {      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, company)          .withValue(ContactsContract.CommonDataKinds.Organization.TYPE, ContactsContract.CommonDataKinds.Organization.TYPE_WORK)          .withValue(ContactsContract.CommonDataKinds.Organization.TITLE, jobTitle)          .withValue(ContactsContract.CommonDataKinds.Organization.TYPE, ContactsContract.CommonDataKinds.Organization.TYPE_WORK)          .build());  }   // Asking the Contact provider to create a new contact                   try {      getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);  } catch (Exception e) {      e.printStackTrace();      Toast.makeText(myContext, "Exception: " + e.getMessage(), Toast.LENGTH_SHORT).show();  }  

Here is the code. Integrate it according to your need. I hope it will help.

like image 142
Khawar Avatar answered Oct 07 '22 03:10

Khawar


These examples are fine, I wanted to point out that you can achieve the same result using an Intent. The intent opens the Contacts app with the fields you provide already filled in.

It's up to the user to save the newly created contact.

You can read about it here: https://developer.android.com/training/contacts-provider/modify-data.html

Intent contactIntent = new Intent(ContactsContract.Intents.Insert.ACTION); contactIntent.setType(ContactsContract.RawContacts.CONTENT_TYPE);  contactIntent         .putExtra(ContactsContract.Intents.Insert.NAME, "Contact Name")         .putExtra(ContactsContract.Intents.Insert.PHONE, "5555555555");  startActivityForResult(contactIntent, 1); 

startActivityForResult() gives you the opportunity to see the result.

I've noticed the resultCode works on >5.0 devices,

but I have an older Samsung (<5) that always returns RESULT_CANCELLED (0).

Which I understand is the default return if an activity doesn't expect to return anything.

@Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) {     super.onActivityResult(requestCode, resultCode, intent);      if (requestCode == 1)     {         if (resultCode == Activity.RESULT_OK) {             Toast.makeText(this, "Added Contact", Toast.LENGTH_SHORT).show();         }         if (resultCode == Activity.RESULT_CANCELED) {             Toast.makeText(this, "Cancelled Added Contact", Toast.LENGTH_SHORT).show();         }     } } 
like image 45
jj. Avatar answered Oct 07 '22 03:10

jj.