2012-06-14 45 views
0

我想要從我的手機使用android聯繫人圖片,姓名,號碼。即時通訊使用ContactsContract來獲取這些信息。但我不能。任何人都可以知道確切的學習教程。我想在自定義列表視圖中列出這些信息。提前致謝。我希望你的寶貴指導能幫助我。如何在Android中獲取聯繫人信息?

PS:在DB中我有我的朋友編號。我想使用匹配的電話號碼同步電話聯繫人和數據庫聯繫人。我需要在列表視圖中顯示所有匹配的聯繫人。列表視圖有ImageView的,姓名,號碼..

+2

但我不能?爲什麼發生了什麼? –

+0

可能的重複[獲取來自android聯繫人選擇器的聯繫信息](http://stackoverflow.com/questions/3044545/get-contact-info-from-android-contact-picker) –

+0

您是否添加了READ_CONTACTS的使用權限在清單文件中?沒有它,它不會工作。 – Pankaj

回答

0
setReminder.numbers.clear(); 
    Intent intent = new Intent(Intent.ACTION_PICK); 
    intent.setType(ContactsContract.Contacts.CONTENT_TYPE); 
    ((Activity) mcontext).startActivityForResult(intent, 1); 

和活動的結果執行這一

if (requestCode == 1 && resultCode == Activity.RESULT_OK) 
    {   
     getContactInfo(intent);   
     } 
} 

protected void getContactInfo(Intent intent) 
{ 
    String phoneNumber = ""; 
    //String email=""; 

    Cursor cursor = ((Activity) mcontext).managedQuery(intent.getData(), null, null, null, null);  
    while (cursor.moveToNext()) 
    {   
      String contactId = cursor.getString(cursor.getColumnIndex(BaseColumns._ID)); 

      String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 


     if (hasPhone.equalsIgnoreCase("1")) 
      hasPhone = "true"; 
     else 
      hasPhone = "false" ; 

     if (Boolean.parseBoolean(hasPhone)) 
     { 
     Cursor phones = ((Activity) mcontext).getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null); 
     //Cursor Email = ((Activity) mcontext).getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,ContactsContract.CommonDataKinds.Email.CONTACT_ID +" = "+ contactId,null, null); 
     while (phones.moveToNext()) 
     { 
      phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
      numbers.add(phoneNumber); 
     } 

     phones.close(); 
     Cursor emailCur = ((Activity) mcontext).getContentResolver().query( 
       ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
       null, 
       ContactsContract.CommonDataKinds.Email.CONTACT_ID +" = "+ contactId, 
       null, null); 


      while (emailCur.moveToNext()) { 
       // This would allow you get several email addresses 
        // if the email addresses were stored in an array 
       email = emailCur.getString(
           emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); 
       String emailType = emailCur.getString(
           emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE)); 
      } 
      emailCur.close(); 

      if(email==null) 
      { 
       email=""; 
      } 
     } 

     }   
    if(phoneNumber != null && phoneNumber.length() > 0){ 

     chooseContactArray.clear(); 
     for(int i=0;i<numbers.size();i++) 
     { 
      chooseContactArray.add(numbers.get(i)); 
     } 
     adapter.notifyDataSetChanged(); 
    } 
    cursor.close(); 

} 
相關問題