-1

I've been looking everywhere since the past few days to find a way to retrieve a contact name using a phone number I already have stored in a variable, unfortunately everything I found so far seems to be using deprecated functions/calls.

Of Course, I tried doing it my own way but I feel like my Android/JAVA knowledge is not good enough to understand this concept yet, keep getting some errors or force close when I try to run anything.

So far the best thing I could find was something like 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 by using this code, Eclipse tells me: context cannot be resolved. A lot of the codes and explanations I found were using this Context thing, but I still don't understand it even after reading this: What is 'Context' on Android?

Any help will be greatly appreciated, Thank you very much

Community
  • 1
  • 1
Takoyaro
  • 928
  • 7
  • 14

1 Answers1

0

If you're using this inside an activity, then a context is what you get by using this. So basically here, instead of calling context.getContentResolver(), call this.getContentResolver() or simply just getContentResolver().

Eclipse complains basically because you're trying to call a method of something called context which Eclipse doesn't know because it hasn't been declared anywhere. It would work if you previously did something like Context context = this;, but that's really useless.

getContentResolver() is a method declared and defined by Activity which is a class that your activity extends, therefore you can call it just like that.

I hope it helps. As to what this context really is, I am sorry, but I can't help you with that as I am not even sure I understand it correctly.

Also, please notice, that I haven't checked the code you posted and I don't know if it works for obtaining a contact's name from a phone number. Just wanted to help you with getting rid of the context cannot be resolved error.

zbr
  • 6,860
  • 4
  • 31
  • 43
  • Wow this is REALLY helpful, all these hours spent on trying to understand, and you made me understand in 30 seconds. Thank a lot Zabri! – Takoyaro May 08 '13 at 19:03
  • Did I? Well, good. :) Would you accept the answer then, please? Thank you. – zbr May 08 '13 at 19:05