Contact picker filtering - android

I use the contact picker in this way:
Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
this.startActivityForResult(intent, PICK_CONTACT_REQUEST);
My question is if somehow the contact list can be filtered? For example I want to see only those contacts in the contact list which have at least a phone number or an email address.

I would suggest to use your custom view for the contacts- it is not rather difficult and you can customize it however you want. I personally implemented that way the functionality you need.
See here:
String PHONE_CONTACTS_ORDER_CLAUSE = ContactsContract.Contacts.DISPLAY_NAME
+ " ASC";
List<PhoneContact> contacts = new ArrayList<PhoneContact>(); // I have defined the bean PhoneContact
String[] projection = { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME }; //Choose the columns you need
Cursor cursor = this.getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, projection, null/* the place for your where clause*/, null/* the place for your where args*/,
PHONE_CONTACTS_ORDER_CLAUSE);
startManagingCursor(cursor);
int contactIdIdx = cursor.getColumnIndex(ContactsContract.Contacts._ID);
int displayNameIdx = cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
while (cursor.moveToNext()) {
PhoneContact contact = new PhoneContact(); // This is a class i defined, use the data the way you like.
contact.setContactId(cursor.getString(contactIdIdx));
contact.setDisplayName(cursor.getString(displayNameIdx));
contacts.add(contact);
}
EDIT
Sorry got distracted when writing the comment: the Contact id is actually the glue between the different content providers of the Contact related data. These are a few more providers you can use to see whether there are any associated phones or emails with the contact:
ContactsContract.CommonDataKinds.Phone.CONTENT_URI
ContactsContract.CommonDataKinds.Email.CONTENT_URI

Related

How to search contacts by name and phone number?

I am using Contacts.CONTENT_FILTER_URI to search for contacts.
Uri contentUri = Uri.withAppendedPath(
ContactsContract.Contacts.CONTENT_FILTER_URI,
Uri.encode(searchString));
The searchstring can be either a number or a name. That works great.
My only problem is that the result does not contain a contact phone number.
I know that I can get it by querying ContactsContract.Data.CONTENT_URI. However, I would like to find a solution that will give me a contact name and phone number with a single query.
You should use Phone.CONTENT_FILTER_URI instead of Contacts.CONTENT_FILTER_URI
Docs say:
The filter is applied to display names as well as phone numbers.
Try this:
Uri filterUri = Uri.withAppendedPath(Phone.CONTENT_FILTER_URI, Uri.encode(searchString));
String[] projection = new String[]{ Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER };
Cursor cur = getContentResolver().query(filterUri, projection, null, null, null);

Call a contact in android

I have wrote an android app that uses voice recognition and save voice into a string.Now i can access to different part of this string and for example when i say call dad i need to retrieve dad's number from contacts.
private void call() {
Intent in=new Intent(Intent.ACTION_CALL,Uri.parse("0000000000"));
try{
startActivity(in);
}
catch (android.content.ActivityNotFoundException ex){
Toast.makeText(getApplicationContext(),"yourActivity is not founded",Toast.LENGTH_SHORT).show();
}
}
I know this function but instead of giving Uri.parse the actual number, i need to pass dad's number to it.For example something like Uri.parse(dad.PhoneNumber).How can i retrieve a phone number from contacts by using a name ? Thanks.
You need to obtain the number from a contact you know only the name, so:
// Define the fields that the query will return
String[] PROJECTION = new String[] {
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER
};
ContentResolver cr = getContentResolver();
// Execute the query and receive the cursor with the results
Cursor cursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " LIKE ?",
new String[] { "Name of the Contact" },
null );
Change the "Name of the contact" with your contact name...
Now you can get the value from cursor
// First discover the index of the desired field (Number)
final int indexNumber = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String number = cursor.getString(indexNumber);
Then you can use your intent.
PS: The example uses a LIKE operator, because of this you can find more than one result in the cursor. Adapt to you necessity.

Cannot get contact details from some contact

I've pulled out most of my hair now and really needs some help
before I go completely bold
I'm trying to launch an action picker to select a Contact that has a phone number.
When that contact is selected, I want to extract the name and phone number.
But this only happens for some contacts, not all.
The code is rougly as follows:
Select contact:
Intent contactPicker = new Intent(Intent.ACTION_PICK, contactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(contactPicker, REQ_PICK_CONTACT);
Extract id (notice getData().getLastPathSegment():
onContactForImportPicked(intent.getData().getLastPathSegment());
and then I try to fetch this contact:
String[] fields = new String[] {
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY
};
Cursor cursor = content.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
fields,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?", new String[] { id }, // SEE BELOW
null);
It this point, many contacts are fetched correctly, but a lot is
also non-existing. cursor.getCount() == 0. In the 'SEE BELOW'
section above, I've tried various other fields, linke
Contact._ID, Phone._ID etc etc.
Any idea why some contacts are not fetched with this method?
When I use your code, I don't even get the contact I selected correctly. The intent data contains the URI to the data you want, so you can use that directly.
Try --
Cursor cursor = content.query(data.getData(),
fields,
null,
null,
null);

Getting only "normal" contacts

Currently in my application I fetch all the contacts with the followign Cursor query.
String[] columnsToReturn = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY
};
Cursor contactsCursor = getActivity().getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, columnsToReturn, null, null, null);
This returns all contacts, including Skype, WhatsApp etc. I don't want these. I only want the "normal" ones. Those that aren't for any apps, just those stored on your Google accounts. How do I do this?
Also, can I exclude the user's own entries? For example, my name appears multiple times on the list for all of my different email addresses.
The ContactsContract.Contacts table contains "consolidated" contacts.
... a record per aggregate of raw contacts representing the same
person.
If you want to query contacts of a particular account or account type, you need to use RawContacts. For example, for Google contacts this would be:
Cursor c = getContentResolver().query(
RawContacts.CONTENT_URI,
new String[] { RawContacts.CONTACT_ID, RawContacts.DISPLAY_NAME_PRIMARY },
RawContacts.ACCOUNT_TYPE + "= ?",
new String[] { "com.google" },
null);
ArrayList<String> myContacts = new ArrayList<String>();
int contactNameColumn = c.getColumnIndex(RawContacts.DISPLAY_NAME_PRIMARY);
while (c.moveToNext())
{
// You can also read RawContacts.CONTACT_ID to read the
// ContactsContract.Contacts table or any of the other related ones.
myContacts.add(c.getString(contactNameColumn));
}

Can't fetch phone numbers for linked contacts when one of the groups is invisible

I faced with problem while fetching phone numbers for linked contacts on HTC phone.
The problem only with contacts that have phone numbers in invisible group.
E.g. I have contact in addressbook that linked from Google and Facebook accounts.
Contact1 - Google account (contact name, email)
Contact1 - Facebook account (contact name, phone number)
In the Contacts settings Google group is active and Facebook group is hidden.
Here what I am doing in the code.
Preparing cursor for ListView that shows only contacts with phone numbers and in visible group.
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.LOOKUP_KEY
};
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
CursorLoader cursorLoader = new CursorLoader(AllContactsNewActivity.this,
uri, projection, ContactsContract.Contacts.HAS_PHONE_NUMBER +" = 1 " +
" AND " + ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'",
selectionArgs, sortOrder);
The Contact1 is presented in the list.
When user click on this contact I am fetching phone numbers from selected contact, but get empty list.
CursorLoader cursorLoader = new CursorLoader(
AllContactsNewActivity.this,
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projection,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?",
new String[] { String.valueOf(contactId) }, null);
I have already tried to use RAW_CONTACT_ID, _ID, LOOKUP_KEY for fetching numbers but w/o luck.
Could some one give me piece of advice how I can fetch phone numbers for these contacts or just not show such contacts in the ListView.
I had this exact same problem and searched for hours to find a solution. This is probably too late for you, but if you use the following code the facebook contacts are not included in the listing.
Please note that this uses deprecated code. I suspect, however, that we are stuck using it until facebook gets their act together and stops violating every rule Google ever made regarding contacts.
Calling code:
Intent intent = new Intent(Intent.ACTION_PICK, Contacts.Phones.CONTENT_URI);
startActivityForResult(intent, SELECT_CONTACT);
Later in onActivityResult
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
startManagingCursor(c);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndexOrThrow(People.NAME));
String number = c.getString(c.getColumnIndexOrThrow(People.NUMBER));
Toast.makeText(this, name + " has number " + number, Toast.LENGTH_LONG).show();
}

Categories

Resources