I am using Intent.ACTION_INSERT_OR_EDIT for inserting/editing contact to mobile phone.
However, I put a ContactsContract.Intents.Insert.NAME to bundle for case when user want to insert a new contact. But the name is inserted also when user choose edit contact (the old name is rewrited) ... is there some FLAG or something how to prevent this behavior ?
Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
intent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
intent.putExtra(ContactsContract.Intents.Insert.NAME,
u.getName());
intent.putExtra(INTENT_KEY_FINISH_ACTIVITY_ON_SAVE_COMPLETED,
true);
startActivityForResult(intent, ADD_CONTACT);
Related
Most developers complain about the difficult API when trying to create an android system contact.
I want to create one with an intent, that is the contact app opens and has filled the appropiate fields from the passed intent data.
I know that I can set the first and the lastname just like this:
intent.putExtra(ContactsContract.Intents.Insert.NAME, "Firstname Lastname");
The builtin contactapp splits the string automatically and sets the first words to the GIVEN_NAME field and the last word corresponds to the FAMILY_NAME field.
But adding the prefix and suffix words into that string dont fill the PREFIX, SUFFIX fields.
This also will have no effect, just as depicted here:
ArrayList<ContentValues> data = new ArrayList<ContentValues>();
ContentValues name = new ContentValues();
name.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
name.put(ContactsContract.CommonDataKinds.StructuredName.PREFIX, "Sir");
name.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, "John");
name.put(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, "Doe");
data.add(name);
intent.putExtra(ContactsContract.Intents.Insert.DATA, data);
startActivity(intent);
So how do I transfer the prefixes and suffixes to the contacts app via an intent?
The thing is, there are no official example codes to work with the contactsContract API, its just so frustrating.
The way you build ArrayList<ContentValues> data seems ok, but then according to the docs it should be added to an intent using:
Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
intent.putParcelableArrayListExtra(Insert.DATA, data);
startActivity(intent);
So the full code would be:
ContentValues name = new ContentValues();
name.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
name.put(StructuredName.PREFIX, "Sir");
name.put(StructuredName.GIVEN_NAME, "John");
name.put(StructuredName.FAMILY_NAME, "Doe");
ArrayList<ContentValues> data = new ArrayList<ContentValues>();
data.add(name);
Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
intent.putParcelableArrayListExtra(Insert.DATA, data);
startActivity(intent);
I want to pass Street, City, State of Address field to create new contact or update existing contact activity.
ArrayList<ContentValues> data = new ArrayList<ContentValues>();
ContentValues row1 = new ContentValues();
row1.put(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE);
row1.put(StructuredPostal.CITY, "mCity");
row1.put(StructuredPostal.COUNTRY, "mCountry");
row1.put(StructuredPostal.STREET, "mStreet");
row1.put(StructuredPostal.POSTCODE, "mPostCode");
data.add(row1);
Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
intent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
intent.putExtra(ContactsContract.Intents.Insert.PHONE, "9XXXXXXXXX");
intent.putExtra(ContactsContract.Intents.Insert.PHONE_TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK);
intent.putExtra(ContactsContract.Intents.Insert.COMPANY, "mCompanyName");
intent.putParcelableArrayListExtra(ContactsContract.Intents.Insert.DATA, data);
startActivity(intent);
But here address is not visible in edit contact widow. Also I don't want to add contact directly using ContentProviderOperation. I want to show first edit contact window with above pre-filled fields.
Is possible to start default people/contact activity and pass structured data like
separated first name and last name, phone type, city, postal code and similar data.
I use following similar code:
Intent addContactIntent = new Intent(Intent.ACTION_INSERT);
addContactIntent.setType(ContactsContract.Contacts.CONTENT_TYPE);
addContactIntent.putExtra(ContactsContract.Intents.Insert.POSTAL, "abc 2343");
startActivity(addContactIntent );
This works fine but I can't specify which is for example postal code, and what is city or place.
I found samples like this here but I cant start add new contact intent before, so user can't edit something before he saves contact. Code immediately saves the contact without user interaction.
Any help would be appreciable.
As for your information you can use ContactsContract.Intents.Insert.DATA ,which is designed to insert multiple data items.Like city,state etc for the address field.But the real problem is it in not available prior to API 11.
Here is how its work.
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);
//Address
ContentValues row4 = new ContentValues();
row4.put(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE);
row4.put(StructuredPostal.CITY, "CityMe");//Pre populating city
row4.put(StructuredPostal.COUNTRY, "COUNTRYme");//Pre populating country
row4.put(StructuredPostal.STREET, "STREETme");////Pre populating street
row4.put(StructuredPostal.POSTCODE, "POSTCODEme");//
data.add(row4);
Intent i = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
i.putParcelableArrayListExtra(Insert.DATA, data);
From my app, I want to invoke the Contacts editor to allow the user to add a new contact. I want to find a way for the app to pass to the editor an account to be used. How can this be done?
I have not found any documentation on how to do this. I did find some code on github which shows a contact editor retrieving account info (see ContactEditorActivity). It calls
getParcelable(Intents.Insert.ACCOUNT);
I think this must be deprecated code though, as I can't find the value Intents.Insert.ACCOUNT anywhere in the references.
Overall, my code for invoking the editor is working; here's an extract:
Intent intent = new Intent();
intent.setAction (Intent.ACTION_INSERT);
intent.setData (ContactsContract.Contacts.CONTENT_URI);
intent.putExtra (ContactsContract.Intents.Insert.NAME, name);
... "put" other values ...
startActivityForResult (intent, ACTIVITY_REQUEST_FULL_EDIT);
Thanks.
I know this is an old question, but here is my solution which seemed to work for me, just in case someone has the same problem.
I was using Xamarin for Android but the Java code is pretty much the same.
For API 23+, use the ExtraAccount field EG:
Android.Accounts.Account account = new Android.Accounts.Account(accountName, accountType);
...
intent.PutExtra(ContactsContract.Intents.Insert.ExtraAccount, account);
...
And for older APIs I used the field "com.android.contacts.extra.ACCOUNT" EG:
Android.Accounts.Account account = new Android.Accounts.Account(accountName, accountType);
...
intent.PutExtra("com.android.contacts.extra.ACCOUNT", account);
...
This opened the contact intent, defaulting to the passed in account (by-passing any phone defaults). This was handy because I was setting a whole bunch of information like url, job title etc and it was getting lost because the contact intent was defaulting to SIM card - which can not hold this data!
In case your are wondering how to get the accounts on the phone, you can use:
AccountManager.Get(context).GetAccountsByType("com.google");
to get google accounts, or
AccountManager.Get(context).GetAccounts();
to get all accounts. However neither include the phone account or SIM account, so I used this code to get these:
using Android.Accounts;
...
public List<Account> GetAccounts()
{
if (CheckReadContactsPermission())
{
var accountList = new List<Account>();
var uri = ContactsContract.Settings.ContentUri;
string[] projection = { ContactsContract.Settings.InterfaceConsts.AccountName,
ContactsContract.Settings.InterfaceConsts.AccountType };
var loader = new CursorLoader(context, uri, projection, null, null, null);
var cursor = (ICursor)loader.LoadInBackground();
if (cursor.MoveToFirst())
{
do
{
accountList.Add(new Account(cursor.GetString(cursor.GetColumnIndex(projection[0])),
cursor.GetString(cursor.GetColumnIndex(projection[1]))));
} while (cursor.MoveToNext());
}
return accountList;
}
return null;
}
I want to display the phone's contact log directly inside my application on a button click, in order to select a contact and its number. Can anybody help me with this?
I got the answer. :)
String myData = "content://contacts/people/";
Intent myActivity2 = newIntent(Intent.ACTION_VIEW, Uri.parse( myData) );
startActivity(myActivity2);
or we can use
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent, 2);