2012-04-26 103 views
0

我正在開發一個應用程序,我需要從提供的名稱對應的聯繫人中獲取電話號碼。我已經嘗試了很多代碼,但他們似乎都沒有工作。從聯繫人獲取基於名稱的電話號碼Android

這是我目前使用現在

public static String getContactPhoneNumber(Context context, String contactName, int type) { 
     String phoneNumber = null; 

     String[] whereArgs = new String[] { contactName, String.valueOf(type) }; 

     Log.d(TAG, String.valueOf(contactName)); 

     Cursor cursor = context.getContentResolver().query(
       ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
       null, 
       ContactsContract.Contacts.DISPLAY_NAME + " = ? and " 
         + ContactsContract.CommonDataKinds.Phone.TYPE + " = ?", whereArgs, null); 

     int phoneNumberIndex = cursor 
       .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER); 

     Log.d(TAG, String.valueOf(cursor.getCount())); 

     if (cursor != null) { 
      Log.v(TAG, "Cursor Not null"); 
      try { 
       if (cursor.moveToNext()) { 
        Log.v(TAG, "Moved to first"); 
        Log.v(TAG, "Cursor Moved to first and checking"); 
        phoneNumber = cursor.getString(phoneNumberIndex); 
       } 
      } finally { 
       Log.v(TAG, "In finally"); 
       cursor.close(); 
      } 
     } 

     Log.v(TAG, "Returning phone number"); 
     return phoneNumber; 
    } 

在通過聯繫人名稱代碼(比如:李四)和式(2這是移動類型爲int值),返回的電話號碼爲空即使聯繫人「John Doe」存在於我的聯繫人列表中。

請幫忙!!!

回答

0

試試這個

,而不是ContactsContract.CommonDataKinds.Phone.CONTENT_URIContactsContract.Contacts.CONTENT_URI參數查詢方法。

相關問題