2011-03-08 205 views
2

我無法訪問聯繫人的聯繫人號碼。 我從網上得到這段代碼訪問聯繫人的聯繫人號碼

Intent intent = new Intent(Intent.ACTION_PICK);   
      intent.setType(ContactsContract.Contacts.CONTENT_TYPE);   
      startActivityForResult(intent, PICK_CONTACT);   
      break;  
      } 
     } 



public void onActivityResult(int requestCode, int resultCode, Intent intent) {  
    if (requestCode == PICK_CONTACT) 
    {     
     Cursor cursor = managedQuery(intent.getData(), null, null, null, null);  
     cursor.moveToNext();  
     String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));   
     String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));   
     Toast.makeText(this, "Contect LIST = "+name, Toast.LENGTH_LONG).show();  

    } 

但是它只顯示聯繫人的名字。 我想要聯繫人的電話號碼。

回答

2

入住這個帖子了,它有答案的尋找:Read all contact's phone numbers in android

+0

上面的代碼片段處理了Intent,而您發佈的鏈接描述了使用api提取聯繫人詳細信息。他們完全不同! – 2011-03-08 14:55:52

+0

兩者都使用從聯繫人查詢返回的遊標。您是否暗示聯繫人的屬性因爲在onActvityResult中進行查詢而發生變化? – Lunchbox 2011-03-08 15:03:59

+0

謝謝!該鏈接還幫助我瞭解其他幾件事情。 :) – bhabs 2011-03-09 05:30:13

0

修改您的代碼段

Intent intent = new Intent(Intent.ACTION_PICK);       
    intent.setType(ContactsContract.Contacts._ID);       
    startActivityForResult(intent, PICK_CONTACT);       
    break;      
      }    
      } 
    public void onActivityResult(int requestCode, int resultCode, Intent intent) 
    {   if (requestCode == PICK_CONTACT)   
    { Cursor cursor = managedQuery(Email.CONTENT_URI, null, Email.CONTACT_ID + " = " + intent.getData(), null, null);     
cursor.moveToNext();     
    String contactId = cursor.getString(cursor.getColumnIndex(Email._ID)); 
    String name = cursor.getString(cursor.getColumnIndexOrThrow(Email.DATA1));     
    Toast.makeText(this, "Contect LIST = "+name, Toast.LENGTH_LONG).show();    
    } 
+0

謝謝!我appriciate您的答覆,但這並沒有幫助我...它仍然顯示聯繫人的名稱,而不是數字! – bhabs 2011-03-09 06:31:15

0
// phone numbers 

    int _contact_id=10111; // your contact id 
    Cursor curPhone = null; 

    // 1 = home ,2= personal(mobile), 3= work 

    try { 

     Uri URI_PHONE = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; 
     String[] PROJECTION_PHONE = new String[]{ 
          ContactsContract.CommonDataKinds.Phone.NUMBER, 
          ContactsContract.CommonDataKinds.Phone.LABEL, 
          ContactsContract.CommonDataKinds.Phone.TYPE}; 
     String SELECTION_PHONE = ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?"; 
     String[] SELECTION_ARRAY_PHONE = new String[]{String.valueOf(_contact_id)}; 

     curPhone = context.getContentResolver() 
       .query(URI_PHONE, PROJECTION_PHONE, SELECTION_PHONE, SELECTION_ARRAY_PHONE, null); 

     if (curPhone.getCount() > 0) { 

     curPhone.moveToFirst(); 
     int indexNumber = curPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); 
     int labelIndex = curPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL); 
     int indexPhoneType = curPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE); 
      while (!curPhone.isAfterLast()) { 
        String contactNumber = curPhone.getString(indexNumber); 
        String labelName = ""; 
        int labelType = curPhone.getInt(indexPhoneType); 
        if (labelType ==ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM) { 
          labelName = curPhone.getString(labelIndex); 
        } 
      android.util.Log.e("[email protected])(@_Number:- ", contactNumber + "[" + labelType + "] " + labelName); 
        curPhone.moveToNext(); 
      } 
     } else { 
        // noPhones = true; 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     if (curPhone != null) { 
      curPhone.close(); 
     } 
} 
0

要獲取聯繫人姓名,號碼以及選定聯繫人的電子郵件從您的聯繫人列表中,然後只需添加此代碼。

private Button btn; 
private String TAG = "Contacts"; 
private static final int RESULT_PICK_CONTACT = 1; 


btn.setOnClickListener(new View.OnClickListener() {  
@Override 
public void onClick(View v) { 
selectSingleContact(); 
} 
}); 

private void selectSingleContact() { 
Intent contactPickerIntent = new  
Intent(Intent.ACTION_PICK,ContactsContract.CommonDataKinds.Phone.CONTENT_URI); 
startActivityForResult(contactPickerIntent, RESULT_PICK_CONTACT); 
}  

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
// check whether the result is ok 
if (resultCode == RESULT_OK) { 
// Check for the request code, we might be usign multiple startActivityForResult 
switch (requestCode) { 
    case RESULT_PICK_CONTACT: 
     contactPicked(data); 
     break; 
    } 
} else { 
Log.e("ContactFragment", "Failed to pick contact"); 
    } 
} 

private void contactPicked(Intent data) { 

Uri uri = data.getData(); 
Log.i(TAG, "contactPicked() uri " + uri.toString()); 
Cursor cursor; 
ContentResolver cr = getActivity().getContentResolver(); 

try { 
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 
if (null != cur && cur.getCount() > 0) { 
    cur.moveToFirst(); 
    for (String column : cur.getColumnNames()) { 
     Log.i(TAG, "contactPicked() Contacts column " + column + " : " + cur.getString(cur.getColumnIndex(column))); 
    } 
} 

if (cur.getCount() > 0) { 
    //Query the content uri 
    cursor = getActivity().getContentResolver().query(uri, null, null, null, null); 

    if (null != cursor && cursor.getCount() > 0) { 
     cursor.moveToFirst(); 
     for (String column : cursor.getColumnNames()) { 
      Log.i(TAG, "contactPicked() uri column " + column + " : " + cursor.getString(cursor.getColumnIndex(column))); 
     } 
    } 

    cursor.moveToFirst(); 
    id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
    Log.i(TAG, "contactPicked() uri id " + id); 
    String contact_id = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)); 
    Log.i(TAG, "contactPicked() uri contact id " + contact_id); 
    // column index of the contact name 
    name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
    // column index of the phone number 
    phoneNo = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
    //get Email id of selected contact.... 
    Log.e("ContactsFragment", "::>> " + id + name + phoneNo); 

    Cursor cur1 = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, 
      ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{contact_id}, null); 

    if (null != cur1 && cur1.getCount() > 0) { 
     cur1.moveToFirst(); 
     for (String column : cur1.getColumnNames()) { 
      Log.i(TAG, "contactPicked() Email column " + column + " : " + cur1.getString(cur1.getColumnIndex(column))); 
      email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); 
     } 

     //HERE YOU GET name, phoneno & email of selected contact from contactlist..... 
     Log.e("setcontactDetails","::>>" + name+"\nPhoneno:" + phoneNo+"\nEmail: " + email); 
    } else { 
     Log.e("setcontactDetails","::>>" + name+"\nPhoneno:" + phoneNo+"\nEmail: " + email); 
    } 
    } 
} catch (IndexOutOfBoundsException e) { 
e.printStackTrace(); 
    } 
} 

同時爲您的AndroidManifest.xml添加權限它適用於我,希望它也適用於您。