2011-12-21 30 views
6

Tôi đã cố gắng lấy số điện thoại của một số liên lạc bằng URI tra cứu của họ, nhưng tôi không nhận được số điện thoại đó để hoạt động.Lấy số điện thoại từ tra cứu URI

Cursor myC = getContentResolver().query(lookupURI, null, null, 
         null, null); 
       String phoneNumber; 
       if (myC.moveToFirst()) { 
        while (myC.moveToNext()) { 
         phoneNumber = myC.getString(myC 
           .getColumnIndex(Phone.NUMBER)); 
         Log.v("t", "phone number is: " + phoneNumber); 
        } 
       } 

nơi lookupURI.toString() là URI này: content://com.android.contacts/contacts/lookup/0r1-304846522C3052482C4A3442423C3248/1

Bất cứ ai biết những gì tôi đang làm sai?

Trả lời

5

Không thể đảm bảo này sẽ làm việc cho 4,0 vì tôi đã không sử dụng nó trong một thời gian nhưng hoạt động tốt trên 2.3.3:

Để có được ContactID, tôi lần đầu tiên được người dùng lựa chọn một số liên lạc :

public void clickSelectContact(View v) { 
    Intent i = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI); 
    startActivityForResult(i, CONTACTS_REQUEST_CODE); 
} 

Khi người dùng đã lựa chọn một số liên lạc nó trở lại với phương pháp này:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if(requestCode == CONTACTS_REQUEST_CODE){ 
     if(resultCode == RESULT_OK){ 
      Uri uri = data.getData(); 
      System.out.println("uri: "+uri); 
      System.out.println("PHONE NUMBER: " + PhoneUtils.getContactPhoneNumber(this, uri.getLastPathSegment())); 
     } 
    } 
} 

nào gọi tĩnh của tôi util lớp:

01.
private static final String TAG = "PhoneUtils"; 

public static String getContactPhoneNumber(Context context, String contactId) { 
    int type = ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE; 
    String phoneNumber = null; 

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

    Log.d(TAG, "Got contact id: "+contactId); 

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

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

    if (cursor != null) { 
     Log.d(TAG, "Returned contact count: "+cursor.getCount()); 
     try { 
      if (cursor.moveToFirst()) { 
       phoneNumber = cursor.getString(phoneNumberIndex); 
      } 
      } finally { 
       cursor.close(); 
      } 
    } 

    Log.d(TAG, "Returning phone number: "+phoneNumber); 
    return phoneNumber; 
} 

Trường hợp contactId = lookupURI.getLastPathSegment();

Rất phức tạp cho một điều đơn giản như vậy! :-(

Tái bút: Bạn có thể cần sự cho phép này trong biểu hiện của bạn:

<uses-permission android:name="android.permission.READ_CONTACTS" /> 
Các vấn đề liên quan