Saturday, July 16, 2011

[android-developers] Getting a contact name given its phone number

Hello everyone, I am trying to retrieve contact names given the
contact phone number. I made a function that should work in all API
versions, by I can't make it work in 1.6 and I can't see the problem,
maybe someone can spot it? Notice that I've replaced it the API
constants for strings so I don't have deprecated warning problems.


public String getContactName(final String phoneNumber)
{
Uri uri;
String[] projection;

if (Build.VERSION.SDK_INT >= 5)
{
uri = Uri.parse("content://com.android.contacts/phone_lookup");
projection = new String[] { "display_name" };
}
else
{
uri = Uri.parse("content://contacts/phones/filter");
projection = new String[] { "name" };
}

uri = Uri.withAppendedPath(uri, Uri.encode(phoneNumber));
Cursor c =
this.getContentResolver().query(uri,projection,null,null,null);

String contactName = "";

if (c.moveToFirst())
{
contactName = c.getString(0);
}

c.close();
c = null;

return contactName;
}

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment