Search a contact with partial search string - android

I want to search the contact list of the android user, by typing just 3-4 digits of a particular number. I have written the following method to do the same, but this is only working when I write complete 10 digit of the number and not less than that. It means it is only working with the exact match.
e.g I want to the number starting 98965... and so on.. but i only gives me a match when I type complete 9896511112
public static void getContactDetails(Context context, String number, int type) {
String[] projection = new String[]{
ContactsContract.PhoneLookup.DISPLAY_NAME,
ContactsContract.PhoneLookup._ID,
ContactsContract.PhoneLookup.LOOKUP_KEY,
ContactsContract.PhoneLookup.PHOTO_URI,
ContactsContract.PhoneLookup.NUMBER};
Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number.trim() + "%"));
Cursor cursor = context.getContentResolver().query(contactUri, null, null, null, null);
while (Common.nonNull(cursor) && cursor.moveToNext()) {
String lookUpKey = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.LOOKUP_KEY));
String mobileNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.NUMBER));
Log.e("I am found", "Found " + number);
}
cursor.close();
}
Please help me to find my mistake.

Ahh finally. One thing that saved my day:
Uri uri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, Uri.encode(partial));
Use "ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI" instead of ContactsContract.PhoneLookup.CONTENT_FILTER_URI, and it will work for you.
Here is my complete example below:
public static void getContactDetails(Context context, String number, int type) {
String[] projection = new String[]{
ContactsContract.PhoneLookup.DISPLAY_NAME,
ContactsContract.PhoneLookup._ID,
ContactsContract.PhoneLookup.LOOKUP_KEY,
ContactsContract.PhoneLookup.PHOTO_URI,
ContactsContract.CommonDataKinds.Phone.NUMBER};
Uri contactUri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, Uri.encode(number.trim()));
Cursor cursor = context.getContentResolver().query(contactUri, projection, null, null, null);
while (Common.nonNull(cursor) && cursor.moveToNext()) {
String lookUpKey = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.LOOKUP_KEY));
String mobileNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
if (mobileNumber.length() > 10) {
mobileNumber = mobileNumber.substring(mobileNumber.length() - 10);
if (mobileNumber.startsWith(number)) {
Log.e("I am found", "I am found " + mobileNumber + displayName);
}
}
}
cursor.close();
}

Related

Method to return contacts phone number always returns null - Android [duplicate]

I am able to retrieve the contact ID, but then later I wish to separately retrieve the phone number based on the contact ID. The code below is returning a null result for the phone number. (I do wish later to retrieve the name and phone number together and populate a view, but I am just trying to get the phone number to work first).
In my onCreate I have this code
String phoneNum = getPhoneNumber(myID);
TextView phoneTextView = (TextView) findViewById(R.id.textViewPhone);
phoneTextView.setText(phoneNum);
This is the method for getPhoneNumber()
protected String getPhoneNumber(String id) {
ArrayList<String> phones = new ArrayList<String>();
Cursor cursor = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null);
while (cursor.moveToNext()) {
phones.add(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
cursor.close();
String phoneNum;
phoneNum = phones.get(0);
return phoneNum;
}//end getPhoneNumber();
}
This produces the error java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0, which I plan on creating some error handling for. But still, I am certain I have the ID from the previous code, so I don't know why the ArrayList returns null. If you would like to see that code, it is also in my onCreate:
Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
if (cursor.getCount() != 0) {
int numContacts = cursor.getCount();
ArrayList<String> idList = new ArrayList<>();
Random rand = new Random();
int randomNum = rand.nextInt(numContacts);
while (cursor.moveToNext()) {
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
idList.add(id);
}
myID = idList.get(randomNum);
String myString = Integer.toString(randomNum);
TextView myTextView = (TextView) findViewById(R.id.textViewID);
myTextView.setText(myString);
if (myID != null) {
myTextView.setText(myID);
} else {
myTextView.setText("Try Again!");
}
} else {
Toast.makeText(getApplicationContext(), "Your have no contacts.", Toast.LENGTH_SHORT).show();
}
cursor.close();
// You can fetch the Contact Number and Email With Following Methods.
String phone = getPhoneNumber(ContactId);
String email = getEmail("" + ContactId);
private String getPhoneNumber(long id) {
String phone = null;
Cursor phonesCursor = null;
phonesCursor = queryPhoneNumbers(id);
if (phonesCursor == null || phonesCursor.getCount() == 0) {
// No valid number
//signalError();
return null;
} else if (phonesCursor.getCount() == 1) {
// only one number, call it.
phone = phonesCursor.getString(phonesCursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
} else {
phonesCursor.moveToPosition(-1);
while (phonesCursor.moveToNext()) {
// Found super primary, call it.
phone = phonesCursor.getString(phonesCursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
break;
}
}
return phone;
}
private Cursor queryPhoneNumbers(long contactId) {
ContentResolver cr = getContentResolver();
Uri baseUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,
contactId);
Uri dataUri = Uri.withAppendedPath(baseUri,
ContactsContract.Contacts.Data.CONTENT_DIRECTORY);
Cursor c = cr.query(dataUri, new String[]{ContactsContract.CommonDataKinds.Phone._ID, ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.IS_SUPER_PRIMARY, ContactsContract.RawContacts.ACCOUNT_TYPE,
ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.LABEL},
ContactsContract.Data.MIMETYPE + "=?",
new String[]{ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE}, null);
if (c != null && c.moveToFirst()) {
return c;
}
return null;
}
private String getEmail(String id) {
String email = "";
ContentResolver cr = getContentResolver();
Cursor emailCur = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (emailCur.moveToNext()) {
// This would allow you get several email addresses
// if the email addresses were stored in an array
email = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
// String emailType = emailCur.getString(
// emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
}
emailCur.close();
return email;
}
I have not been able to successfully retrieve code based on the Contact ID - but this post gave me a clue: Retrieving a phone number with ContactsContract in Android - function doesn't work
I have altered my original code to query on DISPLAY_NAME instead and things are working now. Here is the method I am using to retrieve the phone number:
private String retrieveContactNumber(String contactName) {
Log.d(TAG, "Contact Name: " + contactName);
Cursor cursorPhone = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " = ? AND " +
ContactsContract.CommonDataKinds.Phone.TYPE + " = " +
ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,
new String[]{contactName},
null);
if (cursorPhone.moveToFirst()) {
contactNumber = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
cursorPhone.close();
Log.d(TAG, "Contact Phone Number: " + contactNumber);
return contactNumber;
}

Android - Search contacts with different formats of number

In my application I try to search a contact using the phonenumber. The phonenumber I am searching with is always in the same format ('123456789' for example).
But the following code retrieves not all contacts I expected.
The main issue might be the different format of phonenumbers in my phone: some contacts are saved with '+12 345 6789', the other with '0123 456789'.
Although I tried ContactsContract.PhoneLookup.NORMALIZED_NUMBER my code retrieves only the contacts saved with phonenumbers in the '123456789'-format.
private String getContactDetails(Context context, String number) {
String[] projection = new String[] {
ContactsContract.PhoneLookup.DISPLAY_NAME,
ContactsContract.PhoneLookup._ID,
ContactsContract.PhoneLookup.LOOKUP_KEY};
int len = number.length();
Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number.substring(len-7)));
String selection = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
selection = ContactsContract.PhoneLookup.NORMALIZED_NUMBER + " LIKE %" + number.substring(len-7) + "%";
}
Cursor cursor = context.getContentResolver().query(contactUri, projection, selection, null, null);
String name = null;
if(cursor != null) {
if (cursor.moveToFirst()) {
name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
}
cursor.close();
}
return name;
}
Don't use both PhoneLookup.CONTENT_FILTER_URI with selection, CONTENT_FILTER_URIs are used to search for data using the URI itself, and should not get any selection.
The PhoneLookup.NORMALIZED_NUMBER column is for getting the result back in an e164 format, not for querying.
Try this:
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode("123456789"));
String[] projection = new String[] { PhoneLookup.NUMBER, PhoneLookup.NORMALIZED_NUMBER };
Cursor c = getContentResolver().query(uri, projection, null, null, null);
if (c != null) {
if (c.moveToFirst()) {
String number = c.getString(0);
String e164_number = c.getString(1);
Log.d(TAG, "number=" + number + ", e164=" + e164_number);
} else {
Log.d(TAG, "couldn't find number");
}
}
c.close();

Get CONTACT_ID for a given number

I am trying to create a function that will take a String as an argument and it will return the Contact_ID for the contact that has a phone number like the String provided.
I have already found the following code
private String getContactName (String number)
{
String contactName = "";
ContentResolver context = getContentResolver();
/// number is the phone number
Uri lookupUri = Uri.withAppendedPath(
PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(number));
String[] mPhoneNumberProjection = { PhoneLookup._ID, PhoneLookup.NUMBER, PhoneLookup.DISPLAY_NAME };
Cursor cur = context.query(lookupUri,mPhoneNumberProjection, null, null, null);
try
{
if (cur.moveToFirst())
{
contactName = cur.getString(2);
return contactName;
}
}
finally
{
if (cur != null)
cur.close();
}
return contactName;
}
It returns all contactsName for a given number.
How i can get the contact id from here?
Thanks!
You simply have to replace cur.getString(2) with cur.getString(0). You already have the _ID as part of your projection (which BTW could be reduced to just the _ID).

How to get contact name from number using like in selection in android

Here is the code which iam using
private String getContactNameFromNumber(String number) {
// define the columns I want the query to return
String[] projection = new String[] {
Contacts.Phones.DISPLAY_NAME,
Contacts.Phones.NUMBER };
// encode the phone number and build the filter URI
Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, Uri.encode(number));
// query time
Cursor c = getContentResolver().query(contactUri, projection, null,
null, null);
// if the query returns 1 or more results
// return the first result
if (c.moveToFirst()) {
String name = c.getString(c
.getColumnIndex(Contacts.Phones.DISPLAY_NAME));
return name;
}
// return the original number if no match was found
return number;
}
but this code returns only number exactly equal to contact number.
i want to use like statement so that even last 7 numbers matches i should be able to get the name..
how to write that..?
Retrieving Name from Phones Contacts using Phone Number
private String getContactNameFromNumber(String number) {
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
Cursor cursor = context.getContentResolver().query(uri, new String[]{PhoneLookup.DISPLAY_NAME},null,null,null);
if (cursor.moveToFirst())
{
name = cursor.getString(cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME));
}
return name;
//proceed as you need
}
try this
private String getContactNameFromNumber(String number) {
ContentResolver cr = getContentResolver();
String [] projection = new String []{
ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
};
String selection = ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE ? ";;
String[] selectionArgs = new String[]{"%"+number+ "%"};
Cursor cursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projection,
selection,
selectionArgs,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
//proceed as you need
...
}

Android: Retrieve contact name from phone number

I would like to retrieve the name of a contact associated with an incoming telephone number. As I process the incoming number in the broascastreceiver having a String with the name of the incoming caller would help my project greatly.
I would think this involves a query using the sql WHERE clause as a filter, but do I need to sort the contacts? An example or hint would be of great assistance.
Although this has already been answered, but here is the complete function to get the contact name from number. Hope it will help others:
public static String getContactName(Context context, String phoneNumber) {
ContentResolver cr = context.getContentResolver();
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
Cursor cursor = cr.query(uri, new String[]{PhoneLookup.DISPLAY_NAME}, null, null, null);
if (cursor == null) {
return null;
}
String contactName = null;
if(cursor.moveToFirst()) {
contactName = cursor.getString(cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME));
}
if(cursor != null && !cursor.isClosed()) {
cursor.close();
}
return contactName;
}
[Updating based on Marcus's comment]
You will have to ask for this permission:
<uses-permission android:name="android.permission.READ_CONTACTS"/>
For that you need to use the optimized PhoneLookup provider as described.
Add the permission to AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_CONTACTS"/>
Then:
public String getContactName(final String phoneNumber, Context context)
{
Uri uri=Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,Uri.encode(phoneNumber));
String[] projection = new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME};
String contactName="";
Cursor cursor=context.getContentResolver().query(uri,projection,null,null,null);
if (cursor != null) {
if(cursor.moveToFirst()) {
contactName=cursor.getString(0);
}
cursor.close();
}
return contactName;
}
This was very helpful, here's my final code for retrieving the caller's Name, id, and Photo:
private void uploadContactPhoto(Context context, String number) {
Log.v("ffnet", "Started uploadcontactphoto...");
String name = null;
String contactId = null;
InputStream input = null;
// define the columns I want the query to return
String[] projection = new String[] {
ContactsContract.PhoneLookup.DISPLAY_NAME,
ContactsContract.PhoneLookup._ID};
// encode the phone number and build the filter URI
Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
// query time
Cursor cursor = context.getContentResolver().query(contactUri, projection, null, null, null);
if (cursor.moveToFirst()) {
// Get values from contacts database:
contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup._ID));
name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
// Get photo of contactId as input stream:
Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(contactId));
input = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(), uri);
Log.v("ffnet", "Started uploadcontactphoto: Contact Found # " + number);
Log.v("ffnet", "Started uploadcontactphoto: Contact name = " + name);
Log.v("ffnet", "Started uploadcontactphoto: Contact id = " + contactId);
} else {
Log.v("ffnet", "Started uploadcontactphoto: Contact Not Found # " + number);
return; // contact not found
}
// Only continue if we found a valid contact photo:
if (input == null) {
Log.v("ffnet", "Started uploadcontactphoto: No photo found, id = " + contactId + " name = " + name);
return; // no photo
} else {
this.type = contactId;
Log.v("ffnet", "Started uploadcontactphoto: Photo found, id = " + contactId + " name = " + name);
}
... then just do whatever you want with "input" (their photo as an InputStream), "name", and "contactId".
And here are the docs listing the ~15 or so columns you have access to, just add them to the projection near the start of the code up above:
http://developer.android.com/reference/android/provider/ContactsContract.PhoneLookup.html
This version is the same as Vikram.exe's answer with code to avoid the ANR
interface GetContactNameListener {
void contactName(String name);
}
public void getContactName(final String phoneNumber,final GetContactNameListener listener) {
new Thread(new Runnable() {
#Override
public void run() {
ContentResolver cr = getContentResolver();
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
Cursor cursor = cr.query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);
if (cursor == null) {
return;
}
String contactName = null;
if(cursor.moveToFirst()) {
contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
}
if(cursor != null && !cursor.isClosed()) {
cursor.close();
}
listener.contactName(contactName);
}
}).start();
}
Pass the contact number from which you are getting the call in the following method. This Method will check whether the contact is saved in your mobile or not. If the contact is saved then it will return the contact name otherwise it return a string unknown number
Add this code in your Broadcast receiver class
public String getContactDisplayNameByNumber(String number,Context context) {
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
name = "Incoming call from";
ContentResolver contentResolver = context.getContentResolver();
Cursor contactLookup = contentResolver.query(uri, null, null, null, null);
try {
if (contactLookup != null && contactLookup.getCount() > 0) {
contactLookup.moveToNext();
name = contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
// this.id =
// contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.CONTACT_ID));
// String contactId =
// contactLookup.getString(contactLookup.getColumnIndex(BaseColumns._ID));
}else{
name = "Unknown number";
}
} finally {
if (contactLookup != null) {
contactLookup.close();
}
}
return name;
}
to get Source code visit this link
For that, we can use the PhoneLookup provider to get the names or contact details using the mobile number.
Add the permission to AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_CONTACTS"/>
Add the following custom kotlin method in your activity and call the same with the required mobile number.
fun getContactNameByPhoneNumber(context: Context, phoneNumber: String): String? {
var phone = phoneNumber
if(phoneNumber != null && phoneNumber.length > 0 && phoneNumber[0].equals('+'))
phone = phoneNumber.substring(3)
val projection = arrayOf(
ContactsContract.PhoneLookup.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER
)
val cursor = context.contentResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projection,
ContactsContract.CommonDataKinds.Phone.NUMBER, null, null
) ?: return ""
for (i in 0 until cursor.count) {
cursor.moveToPosition(i)
val nameFieldColumnIndex = cursor
.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME)
val phoneFieldColumnIndex = cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)
if(phone.equals(cursor.getString(phoneFieldColumnIndex)))
return cursor.getString(nameFieldColumnIndex)
}
return "Unknown"
}
For more: https://developer.android.com/training/contacts-provider/retrieve-names

Categories

Resources