Android Intent to create Contact using StructuredName - android

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

Related

How to add more than 3 numbers in a contact in android programatically

I need to modify the contacts App contacts from my application. I am using the following way:
private void UpdateInsertContact()
{
Intent intentInsertEdit = new Intent(Intent.ACTION_INSERT_OR_EDIT);
intentInsertEdit.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
// Add code here to insert extended data, if desired
intentInsertEdit.putExtra(ContactsContract.Intents.Insert.PHONE, "7547874590");
intentInsertEdit.putExtra(ContactsContract.Intents.Insert.NAME, contactName);
intentInsertEdit.putExtra(ContactsContract.Intents.Insert.SECONDARY_PHONE, "7547874590");
intentInsertEdit.putExtra(ContactsContract.Intents.Insert.TERTIARY_PHONE, "7547874590");
// Sends the Intent with an request ID
startActivity(intentInsertEdit);
}
But this way i am able to add only 3 contacts. How do i add more than 3 conatcts.
yes you can add more than three contacts using Intent . but you have to use contentValues and add it to the ArrayList and pass it as an Extra through the intent, as shown below
Intent intentInsertEdit = new Intent(Intent.ACTION_INSERT_OR_EDIT);
intentInsertEdit.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
intentInsertEdit.putExtra(ContactsContract.Intents.Insert.NAME, contactName);
ArrayList<ContentValues> data = new ArrayList<ContentValues>();
//Filling data with phone numbers
ContentValues row = new ContentValues();
row.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
row.put(Phone.NUMBER, "97");
row.put(Phone.TYPE, Phone.TYPE_MOBILE);
data.add(row);
ContentValues row2 = new ContentValues();
row2.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
row2.put(Phone.NUMBER, "9746");
row2.put(Phone.TYPE, Phone.TYPE_WORK);
data.add(row2);
ContentValues row3 = new ContentValues();
row3.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
row3.put(Phone.NUMBER, "974611");
row3.put(Phone.TYPE, Phone.TYPE_HOME);
data.add(row3);
ContentValues row4 = new ContentValues();
row4.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
row4.put(Phone.NUMBER, "97461180");
row4.put(Phone.TYPE, Phone.TYPE_OTHER);
data.add(row4);
intentInsertEdit.putParcelableArrayListExtra(ContactsContract.Intents.Insert.DATA, data);
startActivity(intentInsertEdit);
this way you can add multiple numbers in the same contact, hope this answers your question :)

How to choose the ID for a new contact

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

How can I insert on SMS into inbox with operator name?

I want tosave operator Name also but i can't find any appropriate tag for that Any body please help me on This.
ContentValues values = new ContentValues();
values.put("address", originatingAddress);
values.put("body", sampleContent);
values.put("date", timeStamp);
context.getContentResolver().insert(Uri.parse("content://sms/inbox"), values);
For getting operator name you can use:
TelephonyManager manager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE));
String OperatorName = manager.getNetworkOperatorName();
and then use it in your code as you want OR like below:
ContentValues values = new ContentValues();
values.put("address", originatingAddress);
values.put("body", sampleContent);
values.put("date", timeStamp);
values.put("body", OperatorName);
context.getContentResolver().insert(Uri.parse("content://sms/inbox"), values);

Android SQlite update query

i have an update query where i need to pass multiple parameter. Please guide how to do. Here in place of "id" i want multiple parameters for example name age.
//Code
ContentValues updateCountry = new ContentValues();
updateCountry.put("country_name", "United States");
db.update("tbl_countries", updateCountry, "id=?", new String[] {Long.toString(countryId)})
Try
ContentValues updateCountry = new ContentValues();
updateCountry.put("country_name", "United States");
db.update("tbl_countries", updateCountry, "id=? AND name=? AND age=?", new String[]{Long.toString(countryId),"name_value","age_value"});

How can i show a pre populated postal address in contacts screen with street , city , zip on android

I am using following code for populating contacts in Android:
Intent addContactIntent = new Intent(Intent.ACTION_INSERT);
addContactIntent.setType(ContactsContract.Contacts.CONTENT_TYPE);
addContactIntent.putExtra(ContactsContract.Intents.Insert.NAME, "Store Name");
addContactIntent.putExtra(ContactsContract.Intents.Insert.PHONE, "1-869-270-9099");
addContactIntent.putExtra(ContactsContract.Intents.Insert.POSTAL, "postal address");*
But I also need to populate city, street and zip in postal address. How to populate these fields using the above code? Or any alternate way to do so.
Ok here is what i have done.
ArrayList<ContentValues> data = new ArrayList<ContentValues>();
//Email
ContentValues row1 = new ContentValues();
row1.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);
row1.put(Email.ADDRESS, "ADDRESSme");
data.add(row1);
//Website
ContentValues row2 = new ContentValues();
row2.put(Data.MIMETYPE, Website.CONTENT_ITEM_TYPE);
row2.put(Website.URL, "URLme");
data.add(row2);
//name
ContentValues row3 = new ContentValues();
row3.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
row3.put(StructuredName.DISPLAY_NAME, "NameMe");
data.add(row3);
//Address
ContentValues row4 = new ContentValues();
row4.put(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE);
row4.put(StructuredPostal.CITY, "CityMe");
row4.put(StructuredPostal.COUNTRY, "COUNTRYme");
row4.put(StructuredPostal.STREET, "STREETme");
row4.put(StructuredPostal.POSTCODE, "POSTCODEme");
data.add(row4);
//Phone
ContentValues row5 = new ContentValues();
row5.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
row5.put(Phone.NUMBER, "NUMBERme");
data.add(row5);
Intent i = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
i.putParcelableArrayListExtra(Insert.DATA, data);
But one thing i need to remind you is that Insert.DATA is available only from API level 11.So this solution only works for Android 3.0+.
If you guys have any better solution please let me know.

Categories

Resources