2013-02-04 115 views
0

我想從一些我曾試圖通過查詢檢索其聯繫人的姓名,但我沒有得到result..It將返回本身的數量.....我已經保存該號碼給聯繫人。麻煩檢索聯繫人信息

我試過代碼...

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    String x = getContactNameFromNumber("+918281306132"); 
    System.out.println(x); 
} 
private String getContactNameFromNumber(String number) { 
    // define the columns I want the query to return 

    System.out.println("Entering into getContactNameFromNumber"); 
    String[] projection = new String[] { 
      Contacts.Phones.DISPLAY_NAME, 
      Contacts.Phones.NUMBER }; 

    // encode the phone number and build the filter URI 
    Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, Uri.encode(number)); 

    // query time 
    Cursor c = getContentResolver().query(contactUri, projection, null, 
      null, null); 

    // if the query returns 1 or more results 
    // return the first result 
    if (c.moveToFirst()) { 
     String name = c.getString(c 
       .getColumnIndex(Contacts.Phones.DISPLAY_NAME)); 
     return name; 
    } 

    // return the original number if no match was found 
    return number; 
} 

}

我還添加了讀取手機狀態的權限了。有人請幫我找回聯繫人的名字。

+0

您的代碼似乎確定。你可以嘗試改變'字符串名稱= c.getString(C .getColumnIndex(Contacts.Phones.DISPLAY_NAME));'到 '字符串名稱= c.getString(0);' 。或者你可以試試我的[**片段**](http://pastie.org/6035954)。如果有效,我會添加答案。 –

+0

我曾嘗試更改我的代碼仍然不工作。 – ammukuttylive

+0

你在logcat中看到任何異常嗎? –

回答

0

你可以試試我的片斷:

public static String findContactByNumber(String phoneNumber, 
     ContentResolver cr) { 

    Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, 
      Uri.encode(phoneNumber)); 

    String[] phoneNoProjections = { PhoneLookup._ID, 
      PhoneLookup.DISPLAY_NAME }; 

    Cursor cursor = cr.query(lookupUri, phoneNoProjections, null, null, 
      null); 

    try { 

     if (cursor.moveToFirst()) { 

      return cursor.getString(1); //1 is the display name index. 0 is id. 

     } 

    } finally { 

     if (cursor != null) 

      cursor.close(); 

    } 

    return null; 

} 
0
ContentResolver cr = getContentResolver(); 
     Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, 
       null, null, null); 
     if (cur.getCount() > 0) { 
      while (cur.moveToNext()) { 
       String id = cur.getString(cur 
         .getColumnIndex(ContactsContract.Contacts._ID)); 
       String name = cur 
         .getString(cur 
           .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 

       System.out.println(name + "name"); 

       if (Integer 
         .parseInt(cur.getString(cur 
           .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { 
        // Query phone here. Covered next 

       } 
      } 

此代碼爲我工作

1

不要在你的代碼中的以下變化

Uri contactUri = Uri.withAppendedPath(Phone.CONTENT_URI, Uri.encode(number)); 

if (c.moveToFirst()) { 
    String name = c.getString(c 
      .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
    return name; 
} 

試試吧,希望它會幫你。 :)