How to search contacts by name and phone number? - android

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

Related

google libphonenumber get only mobile numbers from contacts

I'm using libphonenumber to format phonenumbers in contacts. Is there any way to display only mobile numbers like WhatsApp? no local phone numbers.
The only idea I have is to have a list with all mobile area codes like https://en.wikipedia.org/wiki/List_of_mobile_phone_number_series_by_country and check every number but this is a bit complicated I think.
found the answer...
PhoneNumberUtil.PhoneNumberType nrtype = phoneUtil.getNumberType(NumberProto);
if (nrtype.name() == PhoneNumberUtil.PhoneNumberType.MOBILE) {
}
Uri personUri = ContentUris.withAppendedId(People.CONTENT_URI, personId);
Uri phonesUri = Uri.withAppendedPath(personUri, People.Phones.CONTENT_DIRECTORY);
String[] proj = new String[] {Phones._ID, Phones.TYPE, Phones.NUMBER, Phones.LABEL}
Cursor cursor = contentResolver.query(phonesUri, proj, null, null, null);

Contact picker filtering

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

How to display numbers from contact list?

I use the code below to display contact numbers, but it is displaying names and numbers from the contact list.
public void getContacts(Context context){
context.startActivity(new Intent(Intent.ACTION_PICK, People.CONTENT_URI));
}
Like the above code how can I use the below code for getting only contact numbers from the contact list? I need to display only contact numbers without showing names. Please help with the right solution to solve this problem.
Uri personUri = ContentUris.withAppendedId(People.CONTENT_URI, personId);
Uri phonesUri = Uri.withAppendedPath(personUri, People.Phones.CONTENT_DIRECTORY);
String[] proj = new String[] {Phones._ID, Phones.TYPE, Phones.NUMBER, Phones.LABEL}
Cursor cursor = contentResolver.query(phonesUri, proj, null, null, null);
Try and look at this site:
Query contacts.
There are multiple methods for retrieving different content from contacts.
Hopefully that could help.

contacts in android

I want to get all phone contacts from device in android.i have used the following code.but the problem is it takes more time to return the results.is there any solution?
ContentResolver cr = getContentResolver();
int index=0;
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0)
{
phoneNames=new String[cur.getCount()];
phoneNumbers=new String[cur.getCount()];
while (cur.moveToNext())
{
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
name = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
phoneNames[index]=name;
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext())
{
phoneIndex++;
phoneNumbers[index] = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
index++;
}
pCur.close();
}
}
After reading the code i assume that what you want is a list of contacts with DISPLAY NAMES and their respective phone numbers.
If you are specifically looking for data related to phone numbers i suggest you query on
android.provider.ContactsContract.PhoneLookup and fetch the results using a single cursor.
The following are the fields that you would be interested in:
DISPLAY_NAME
HAS_PHONE_NUMBER
NUMBER
TYPE
e.g
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
resolver.query(uri, new String[]{PhoneLookup.DISPLAY_NAME,...
Further details please refer this
Please post your requirement if the assumptions are not true.
Some of quick checks:
Select only the required columns and not all in the first query.
Instead of using Integer.parseInt(cur.getString) use
cur.getInt()
Use PhoneLookup whenever dealing with phone numbers ( the number
field gives the raw phone number
instead of the value stored in
the database which can contain
-,),( appended with it)
Avoid using Cursor within a cursor. Use the API's which includes
joins already implemented in it like
RawContactsEntity, PhoneLookup.
Hope that helps.
Don't do complex database queries on the UI thread. What are you trying to do with the results? If you are displaying things in a list, use a CursorAdapter so that you only pull out what you need when you need it.

How to look-up a contact's name from their phone number on Android?

I am trying to get the sender's name from the contacts database using a content provider.
The problem is I don't know how to implement it. Like now I can only pull the phone number from the smsMessage. I need to check to see if the phone number that is calling is in the users contacts first and if it is display the name if it is not then display the number.
Yes, this is possible using ContactsContract.PhoneLookup.CONTENT_FILTER_URI in Android 2.0 and higher and Contacts.Phones.CONTENT_FILTER_URL in Android 1.6 and earlier.
For example usage, see the documentation for ContactsContract.PhoneLookup. Excerpt below:
// Android 1.6 and earlier (backwards compatible for Android 2.0+)
Uri uri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, Uri.encode(phoneNumber));
// Android 2.0 and later
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
// Query the filter URI
String[] projection = new String[]{ PhoneLookup.DISPLAY_NAME, ...
Cursor cursor = context.getContentResolver().query(uri, projection, ...
UPDATE: The format of the phone number does not matter. Comparison is robust and highly optimized on Android; it's done using a native sqlite function named PHONE_NUMBERS_EQUAL. For more details, search the codebase for this method. By the way, I'm not certain if it's safe to use that function directly in your own apps, but I wouldn't.
Here's what I did
ContentResolver localContentResolver = this.mContext.getContentResolver();
Cursor contactLookupCursor =
localContentResolver.query(
Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(phoneNumber)),
new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID},
null,
null,
null);
try {
while(contactLookupCursor.moveToNext()){
String contactName = contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
String contactId = contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup._ID));
Log.d(LOGTAG, "contactMatch name: " + contactName);
Log.d(LOGTAG, "contactMatch id: " + contactId);
}
} finally {
contactLookupCursor.close();
}
Passing in your phone number you already have into Uri.encode(phoneNumber)

Categories

Resources