How to display contact list programmatically in android? - android

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);

Related

Android ContactsContract How to fill Prefix, Suffix structuredname fields via new Intent?

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);

how to send a string to listView in another activity ? in android

Good evening guys,
I'm making an app and I want to know how to send a string to "List-View" in another activity ?
You can send data using the following code -
Intent intent = new Intent(this,newActivity);
intent.putExtra(name, value)
name = The name of the extra data
value = The String data value.
startActivity(intent);
In the new activity, you receive string via following (in onCreate)
Intent intent = getIntent();
String str = intent.getString(name)
name = The name of the extra data
Now search the web on how to add a string to list view. You will find it easily

ACTION_INSERT_OR_EDIT put NAME

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);

How to programmatically open contacts pick with search filter entered programmatically?

I want to open Contacts pick activity from my application with search field should be filled programmatically.
Can anyone suggest what URI shoud i use or anything to put in intent's extra?
private static final int PICK_CONTACT_SUBACTIVITY = 2;
private void startContactActivity() {
Uri uri = Uri.parse("content://contacts/people");
// Here in this normally we pass number e.g. Uri.encode("987") but i want to pass name as filter is it possible?
// I have also tried
//uri = Uri.withAppendedPath(android.provider.ContactsContract.Contacts.CONTENT_FILTER_URI, Uri.encode("pra"));
//uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode("pra"));
uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_FILTER_URI, Uri.encode("pra"));
Intent intent = new Intent(Intent.ACTION_PICK, uri);
startActivityForResult(intent, PICK_CONTACT_SUBACTIVITY);
}
Can anyone suggest how can i achieve this?
You can follow :
how-to-pick-contact-number-from-phone-book-of-android-in-to-my-application
or
developer.android.com/training/contacts-provider/modify-data
It seems that it is not possible to specify a filter programmatically.
The Android SDK documentation states (in the chapter "Retrieval and modification with intents") that for ACTION_PICK you can only use one of
Contacts.CONTENT_URI, Phone.CONTENT_URI, StructuredPostal.CONTENT_URI, Email.CONTENT_URI. This is indirectly a filter (all contacts, all with phone numbers, all with postal address, or all with email), but it is limited to this.

Android intent to call a contact, not a number

Context:
I wrote a small easy dialer for android which dials telephone numbers
for contacts.
I use Intent.ACTION_CALL and pass in "tel:" for contacts with just one tel number or with a preferred default number
Within the app I have the contact's lookup_key so that I can access the contact
Question:
I want to initiate a call to a Contact not to a number so that Android:
if contact has 2+ numbers: will fire up a phone number selection dialog and then initiate the call
if the contact has just 1 number: just initiate the call
I could of course implement a popup from scratch but I'd prefer to delegate to a standard action so that the user has the very same UX as using the standard dialer.
You can use something like this to prompt the user to choose the contact, then which phone number to call (if there is more than one)... than pass that to the intent to make the call:
Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, 1);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri contactData = data.getData();
String theID = contactData.toString());
//MAKE YOUR CALL .. do whatever... example:
ContentResolver contentResolver = getContentResolver();
Uri contactData = Uri.parse(theID);
Cursor cur = contentResolver.query(contactData, null, null, null, null);
String theNumber = cur.getString(cur.getColumnIndex("data4"));
cur.close();
Intent my_callIntent = new Intent(Intent.ACTION_CALL);
my_callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
my_callIntent.setData(Uri.parse("tel:" + theNumber));
startActivity(my_callIntent);
}
}
Its not pretty or perfect, probably needs some modifications, just kinda going off the top of my head but hopefully you get the idea.

Categories

Resources