Inserting contacts in Android 2.2
I thought this Q was long forgotten but Since someone upvoted it, I am assuming someone else also faces the same problem as me. After a little struggle I was able to figure out the problem and insert contacts, Hope this helps, here is the sample code:
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();
ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
.withValue(RawContacts.ACCOUNT_TYPE, null)
.withValue(RawContacts.ACCOUNT_NAME,null )
.build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(Data.MIMETYPE,Phone.CONTENT_ITEM_TYPE)
.withValue(Phone.NUMBER, "9X-XXXXXXXXX")
.build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(Data.MIMETYPE,StructuredName.CONTENT_ITEM_TYPE)
.withValue(StructuredName.DISPLAY_NAME, "Mike Sullivan")
.build());
ContentProviderResult[] res = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
How to add the new contacts on android 2.2?
Take a look at: http://developer.android.com/resources/articles/contacts.html
The process involves some steps, as you would insert the name contact+name first, then field by field.
Example for phone number:
import android.provider.ContactsContract.CommonDataKinds.Phone;
...
ContentValues values = new ContentValues();
values.put(Phone.RAW_CONTACT_ID, rawContactId);
values.put(Phone.NUMBER, phoneNumber);
values.put(Phone.TYPE, Phone.TYPE_MOBILE);
Uri uri = getContentResolver().insert(Phone.CONTENT_URI, values);
Additionally, have a read here: http://developer.android.com/reference/android/provider/ContactsContract.CommonDataKinds.html
unable to insert new info to existing contacts on android 2.2.1 froyo
Do you have Samsung SmartPhone?
vnd.sec.contact.phone is default user account using Samsung smartphones.
Samsung smartphone has two Contact type; one is Samsung default phonebook ans the other is Google account.
vnd.sec.contact.phone means 'the contacts contains Samsung default phonebook'.
And I guess the device vendor(Samsung) customize the Contacts Applications.
that'sthe reason thar the code do not run.
HTH.
Is it possible to insert the two new contacts with same display name on Android 2.2?
Android's 2.x Contacts API will aggregate 2 raw contacts with the same name by default. See the section Automatic aggregation in Using the Contacts API.
What you need to do is disable automatic aggregration, which is also described in that very useful article. You merely have to set your RawContact record's AGGREGATION_MODE value to AGGREGATION_MODE_DISABLED.
How to avoid duplicates while inserting the contacts into android 2.2?
Do a lookup for the contact first? Also, note that Android will try to aggregate contacts with similar characteristics.
How to Add Contacts to Contacts Table
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()
Related Topics
Android 'Unable to Add Window -- Token Null Is Not for an Application' Exception
Smoothscrolltopositionfromtop() Is Not Always Working Like It Should
Proguard Missing Type Parameter
Animation in Notification Bar Custom View
Checking a Checkbox in Listview Makes Other Random Checkboxes Checked Too
Android Linkify Text - Spannable Text in Single Text View - as Like Twitter Tweet
Android Unknown Command 'Crunch'
Can't Upload Updated APK to Google Play Due to Different Certificate
Doze Mode and Foreground Service
Understanding Canvas and Surface Concepts
Receiver Not Registered Exception Error
Load Large Images with Picasso and Custom Transform Object
Generating Google Map Release API Key
How How to Do Ussd Requests on Android
Register to Be Default App for Custom File Type
Cannot Start Activity Background in Android 10 [ Android Q ]