2014-08-29 162 views
2

我想獲取聯繫人(保存在電話簿中)手機號碼,工作號碼和家庭號碼。我想在我的3個edittext視圖中設置這些數字。這個怎麼做?這裏是我的代碼獲取聯繫人的手機號碼,工作號碼和家庭號碼

Cursor phones = getActivity().getContentResolver().query(
     Phone.CONTENT_URI, 
     null, 
     Phone.CONTACT_ID + " = " + phoneId, 
     null, 
     null 
); 
while (phones.moveToNext()) { 
    number = phones.getString(phones.getColumnIndex(Phone.NUMBER)); 
    int type = phones.getInt(phones.getColumnIndex(Phone.TYPE)); 
    if (type == Phone.TYPE_HOME) { 
     number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA)); 
     return number; 
    } 
    if (type == Phone.TYPE_MOBILE) { 
     number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA)); 
     return number; 
    } 
    if (type == Phone.TYPE_WORK) { 
     number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA)); 
     return number; 
    } 
} 

回答

1

嘗試this鏈接以獲得接觸的細節,如PHONENUMBER,從電話簿名稱。 並檢查通過發佈喬恩

public List<Person> getContactList(){ 
    ArrayList<Person> contactList = new ArrayList<Person>(); 

    Uri contactUri = ContactsContract.Contacts.CONTENT_URI; 
    String[] PROJECTION = new String[] { 
      ContactsContract.Contacts._ID, 
      ContactsContract.Contacts.DISPLAY_NAME, 
      ContactsContract.Contacts.HAS_PHONE_NUMBER, 
    }; 
    String SELECTION = ContactsContract.Contacts.HAS_PHONE_NUMBER + "='1'"; 
    Cursor contacts = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, PROJECTION, SELECTION, null, null); 


    if (contacts.getCount() > 0) 
    { 
     while(contacts.moveToNext()) { 
      Person aContact = new Person(); 
      int idFieldColumnIndex = 0; 
      int nameFieldColumnIndex = 0; 
      int numberFieldColumnIndex = 0; 

      String contactId = contacts.getString(contacts.getColumnIndex(ContactsContract.Contacts._ID)); 

      nameFieldColumnIndex = contacts.getColumnIndex(PhoneLookup.DISPLAY_NAME); 
      if (nameFieldColumnIndex > -1) 
      { 
       aContact.setName(contacts.getString(nameFieldColumnIndex)); 
      } 

      PROJECTION = new String[] {Phone.NUMBER}; 
      final Cursor phone = managedQuery(Phone.CONTENT_URI, PROJECTION, Data.CONTACT_ID + "=?", new String[]{String.valueOf(contactId)}, null); 
      if(phone.moveToFirst()) { 
       while(!phone.isAfterLast()) 
       { 
        numberFieldColumnIndex = phone.getColumnIndex(Phone.NUMBER); 
        if (numberFieldColumnIndex > -1) 
        { 
         aContact.setPhoneNum(phone.getString(numberFieldColumnIndex)); 
         phone.moveToNext(); 
         TelephonyManager mTelephonyMgr; 
         mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 
         if (!mTelephonyMgr.getLine1Number().contains(aContact.getPhoneNum())) 
         { 
          contactList.add(aContact); 
         } 
        } 
       } 
      } 
      phone.close(); 
     } 

     contacts.close(); 
    } 

    return contactList; 
} 

的答案,Person類

public class Person { 
    String myName = ""; 
    String myNumber = ""; 

    public String getName() { 
     return myName; 
    } 

    public void setName(String name) { 
     myName = name; 
    }  

    public String getPhoneNum() { 
     return myNumber; 
    } 

    public void setPhoneNum(String number) { 
     myNumber = number; 
    } 
} 

希望這可以幫助你。