title: Get contact name given a phone number in Android
body: <p>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? </p>
  
  <p>Note that, I've replaced the API constants for strings so I don't have deprecated warning problems. </p>
  
  <pre><code>public String getContactName(final String phoneNumber) 
  {  
      Uri uri;
      String[] projection;
  
      if (Build.VERSION.SDK_INT &gt;= 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 = this.getContentResolver().query(uri, projection, null, null, null); 
  
      String contactName = "";
  
      if (cursor.moveToFirst()) 
      { 
          contactName = cursor.getString(0);
      } 
  
      cursor.close();
      cursor = null;
  
      return contactName; 
  }
  </code></pre>