I need to add the new contacts on android 2.2 version.
How to add the all fields like Firstname, Lastname, URL, Nickname, IM, Addresses and mobile number?
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
Related
I'm working on an app that create contact using Intent. Why using Intent you will ask : Because It's to let user modify the contact as they want before registering.
My problem is while I'm using StructuredName or StructuredPostal, for other CommonDataKinds it's working well. Here's my code:
ContentValues name = new ContentValues();
name.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
name.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, "Firstname");
name.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
name.put(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, "LASTNAME");
data.add(name);
ContentValues row1 = new ContentValues();
row1.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE);
row1.put(ContactsContract.CommonDataKinds.Organization.COMPANY, "company");
row1.put(ContactsContract.CommonDataKinds.Organization.TITLE, "position");
data.add(row1);
ContentValues row3 = new ContentValues();
row3.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE);
row3.put(ContactsContract.CommonDataKinds.Website.TYPE, ContactsContract.CommonDataKinds.Website.TYPE_HOME);
row3.put(ContactsContract.CommonDataKinds.Website.URL, "website");
data.add(row3);
Here is StructuredPostal
ContentValues row5 = new ContentValues();
row5.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE);
row5.put(ContactsContract.CommonDataKinds.StructuredPostal.CITY, "city");
row5.put(ContactsContract.CommonDataKinds.StructuredPostal.STREET, "street");
row5.put(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, "postcode");
row5.put(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, "country");
data.add(row5);
i = new Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI);
i.putParcelableArrayListExtra(ContactsContract.Intents.Insert.DATA, data);
startActivity(i);
Due to this problem I momentarily did a patch :
i.putExtra(ContactsContract.Intents.Insert.NAME, "Firstname LASTNAME");
i.putExtra(ContactsContract.Intents.Insert.POSTAL, "street, code city, country");
Someone knows what's wrong?
Thanks for answers :)
For the StructuredPostal add the type. And use a different mimetype. Do not add null values for the address fields like street etc.
ContentValues row5 = new ContentValues();
row5.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE);
row5.put(ContactsContract.CommonDataKinds.StructuredPostal.CITY, "city");
row5.put(ContactsContract.CommonDataKinds.StructuredPostal.STREET, "street");
row5.put(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, "postcode");
row5.put(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, "country");
row5.put(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME);
data.add(row5);
StructuredName does not work for me either.
For using StructuredName in the Intent.INSERT you should add both Insert.Name and data row for StructuredName
i.putExtra(ContactsContract.Intents.Insert.NAME, "Firstname LASTNAME");
ContentValues name = new ContentValues();
name.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
name.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, "Firstname");
name.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
name.put(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, "LASTNAME");
data.add(name);
Same goes for StructuredPostal. You need to add Insert.POSTAL and the StructuredPostal data row.
The Insert.Name is taken as the DISPLAY_NAME and the rest of the fields are used for StructuredName. The Insert.POSTAL is used as formatted address. This is why you need to provide both data.
To see how android processes your Intent, check method parseStructuredNameExtra in this file
I want to create a contact in the device's address book.
The answer to the question here and the documentation indicates that we must provide the ID for the contact.
Here is the example from the docs:
ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, "1-800-GOOG-411");
values.put(Phone.TYPE, Phone.TYPE_CUSTOM);
values.put(Phone.LABEL, "free directory assistance");
Uri dataUri = getContentResolver().insert(Data.CONTENT_URI, values);
How can I choose/generate this ID ? if I choose a random value, it might conflict with an existing contact ?
if you looked at the Documentation
you will find a table describes all fields, among them is RAW_CONTACT_ID, i quote
The id of the row in the ContactsContract.RawContacts table that this data belongs to.
so this is the ID of an inserted record at RawContacts table, as if it's master detail schema
check this link for more info about RawContacts
so what i think is you have to insert a rawContact first, get that (automatically generated id)
and use it for inserting a contact
something like this
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 insertedRawContactId = ContentUris.parseId(rawContactUri);
then use insertedRawContactId to insert contact.
ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, insertedRawContactId );
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, "1-800-GOOG-411");
values.put(Phone.TYPE, Phone.TYPE_CUSTOM);
values.put(Phone.LABEL, "free directory assistance");
Uri dataUri = getContentResolver().insert(Data.CONTENT_URI, values);
check this site for more info to make it clear.
and regarding the 3 types or 3 elements of contact, check this image
and check this answer points 1,2,3,... etc
hope this help you more understanding
try like this,
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();
ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, rawContactInsertIndex);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, "1-800-GOOG-411");
values.put(Phone.TYPE, Phone.TYPE_CUSTOM);
values.put(Phone.LABEL, "free directory assistance");
Uri dataUri = getContentResolver().insert(Data.CONTENT_URI, values);
hope it will help you
I am developing an SMS backup and restore app. I am able to backup the SMSs into a CSV file. However, when I try to restore the SMSs, the SMSs are stored perfectly into the SMS database. But they are not visible into the Messaging App in Android.
I am using the Content Provider for backup and restoring the SMSs. Following is the code I am using for Restoring the SMSs.
CSVReader reader = new CSVReader(new FileReader(csvFile));
Uri uri = Uri.parse("content://sms/");
String[] nextLine;
while ((nextLine = reader.readNext()) != null) {
ContentValues cv = new ContentValues();
cv.put("_id", nextLine[0]);
cv.put("thread_id", nextLine[1]);
cv.put("address", nextLine[2]);
cv.put("person", nextLine[3]);
cv.put("date", nextLine[4]);
cv.put("protocol", nextLine[5]);
cv.put("read", nextLine[6]);
cv.put("status", nextLine[7]);
cv.put("type", nextLine[8]);
cv.put("reply_path_present", nextLine[9]);
cv.put("subject", nextLine[10]);
cv.put("body", nextLine[11]);
cv.put("service_center", nextLine[12]);
cv.put("locked", nextLine[13]);
this.getContentResolver().insert(uri, cv);
}
Please Let me know, what mistake I am doing. I am trying to work things out for past 1 week, still I don't know my Mistake in the Code. What am I missing in the code?
Do not include _id and thread_id in your inserts. After all records are inserted, do the following to trigger the conversations table threads update:
getContentResolver().delete(Uri.parse("content://sms/conversations/-1", null, null);
I have added one contact to android by following code.
ContentValues values = new ContentValues();
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");
getContentResolver().insert(Data.CONTENT_URI, values);
It shows up on emulator 2.1, but when i am going to delete it manually by "delete contact" option, its not deleteing from emulator.
If I edit some thing on it then only it deletes.
How can i directly delete it from menu ?
Thanks in advance...
Could you please use the below code to add the contact. This will not affect your emulator to delete contact from menu without editing the same.
import android.provider.Contacts.People;
public void addvaluestocontent()
{
ContentValues values = new ContentValues();
values.put(People.NAME, "Abraham Lincoln");
values.put(People._ID, "1");
values.put(People.NUMBER, "23333");
Uri uri = getContentResolver().insert(People.CONTENT_URI, values);
}
You have to save one more field, either "Given Name" or "Family Name". You can test it manually by saving contacts. First try to save manaully only number and then with saving contacts with both name and number.
Simple, Delete the .db file all contacts were deleted. and android will create new file automatically.
path for .db is:
data/data/com.android.providers.contacts/database/contacts.db
Use this method to Check your SDK version and Get the Contacts content Uri. After, that you can insert the contacts with this new Content Uri,
static
{
int sdk=new Integer(Build.VERSION.SDK).intValue();
if (sdk>=5) {
try {
Class<?> clazz=Class.forName("android.provider.ContactsContract$Contacts");
CONTENT_URI=(Uri)clazz.getField("CONTENT_URI").get(clazz);
}
catch (Throwable t) {
Log.e("PickDemo", "Exception when determining CONTENT_URI", t);
}
}
else {
CONTENT_URI=Contacts.People.CONTENT_URI;
}
}
Refer, CommonsWare Examples for Contacts Content Uri. This may be helps you.
I have written an application and added 2 contacts on emulator, but i
am not able to update their names on android 2.1, code is working on
android 1.6 platform with the following code.
ContentValues contactValues = new ContentValues();
contactValues.put(Contacts.People.NAME, firstName+" "+lastName);
getContentResolver().update(UpdateContactUri, contactValues, null,
null);
In android 1.6 i am getting Uri for those two contacts are "content://
contacts/people/1" and "content://contacts/people/2".
but in 2.1 I am getting these values are "content://contacts/people/8"
and "content://contacts/people/9" and while updating its giving
"java.IllegalArgumentException, Empty values" exception.
When i tried to put a static Uri like "content://contacts/people/1",
code was debugged sucessfully but contact was not updated.
How can i resolve it, why i am not getting uri like 1.6 platform ?
Thanks in advance...
in android 2.1, I use this hack code to update contact name:
public static void modifyPeopleName(ContentResolver cr, String id,
String sName) {
if (sName == null)
return;
ContentValues values = new ContentValues();
int android_sdk_version = Integer.parseInt(Build.VERSION.SDK);
if (android_sdk_version < 7) {
values.put(People.NAME, sName);
cr.update(People.CONTENT_URI, values, People._ID+"="+id, null);
} else {
values.put("data1", sName);
cr.update(Uri.parse("content://com.android.contacts/data/"),
values, "raw_contact_id=" + id, null);
}
return;
}
the 2.1 SDK contains new contentHandler for contacts named ContactsContract
the query now moved to look a it different so i'm sure the URI is different too.
We work in 2.1 only an able to edit and get contact's fields.
see http://developer.android.com/reference/android/provider/ContactsContract.html
You can use below code to add contacts in emulator.
import android.provider.Contacts.People;
public void addvaluestocontent()
{
ContentValues values = new ContentValues();
values.put(People.NAME, "Abraham Lincoln");
values.put(People._ID, "1");
values.put(People.NUMBER, "23333");
Uri uri = getContentResolver().insert(People.CONTENT_URI, values);
}