android - Retrieve Contact name from phone number -


i've been looking everywhere since past few days find way retrieve contact name using phone number have stored in variable, unfortunately found far seems using deprecated functions/calls.

of course, tried doing own way feel android/java knowledge not enough understand concept yet, keep getting errors or force close when try run anything.

so far best thing find this:

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 cursor = context.getcontentresolver().query(uri, projection, null, null, null);           string contactname = "";          if (cursor.movetofirst())          {              contactname = cursor.getstring(0);         }           cursor.close();         cursor = null;          return contactname;      } 

but using code, eclipse tells me: context cannot resolved. lot of codes , explanations found using context thing, still don't understand after reading this: what 'context' on android?

any appreciated, thank much

if you're using inside activity, context using this. here, instead of calling context.getcontentresolver(), call this.getcontentresolver() or getcontentresolver().

eclipse complains because you're trying call method of called context eclipse doesn't know because hasn't been declared anywhere. work if did context context = this;, that's useless.

getcontentresolver() method declared , defined activity class activity extends, therefore can call that.

i hope helps. context is, sorry, can't not sure understand correctly.

also, please notice, haven't checked code posted , don't know if works obtaining contact's name phone number. wanted getting rid of context cannot resolved error.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -