Saturday, July 16, 2011

[android-developers] Getting contact name by phone number

Hello everyone, I am trying to retrieve the contact name given a
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 sport it? Notice I replace it the API constants for
strings so I don't have deprecated warning problems.

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

if (Build.VERSION.SDK_INT >= 5)
{
uri = Uri.withAppendedPath(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 cursor = this.getContentResolver().query(uri, projection,
null, null, null);

String contactName = "";

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

cursor.close();
cursor = 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