2012-04-26 14 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; 
    } 

を使用しているコードで、タイプ(モバイル型のint値である2)、返された電話番号はnullです私の連絡先リストに「John Doe」という連絡先が存在しています。

助けてください!

答えて

0

ではなく、メソッドを照会するContactsContract.CommonDataKinds.Phone.CONTENT_URIパス ContactsContract.Contacts.CONTENT_URIパラメータのこの

を試してみてください。

関連する問題