1. 程式人生 > >獲取android手機了通訊錄和sim卡聯絡人

獲取android手機了通訊錄和sim卡聯絡人

private ArrayList<SamContact> getAllContacts()
{
ArrayList<SamContact> arrayList = new ArrayList<SamContact>();
//獲取本機聯絡人
Cursor cur = getContentResolver().query(  
                ContactsContract.Contacts.CONTENT_URI,  
                null ,  
                null ,  
                null ,  
                ContactsContract.Contacts.DISPLAY_NAME  
                        + " COLLATE LOCALIZED ASC" );
if(cur.moveToFirst())
{
do{
SamContact samContact = new SamContact();
samContact.isChoosed = false;
int  idColumn = cur.getColumnIndex(ContactsContract.Contacts._ID);  
           int  displayNameColumn = cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
           // 獲得聯絡人的ID號   
           String contactId = cur.getString(idColumn);  
           // 獲得聯絡人姓名   
           String disPlayName = cur.getString(displayNameColumn);
           System.out.println(disPlayName);
           samContact.name = disPlayName;
           // 檢視該聯絡人有多少個電話號碼。如果沒有這返回值為0   
           int  phoneCount = cur.getInt(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
           if(phoneCount <1)
           {
            continue;
           }
           Cursor phones = getContentResolver().query(  
                   ContactsContract.CommonDataKinds.Phone.CONTENT_URI,  
                   null ,  
                   ContactsContract.CommonDataKinds.Phone.CONTACT_ID  
                           + " = "  + contactId,  null ,  null );
           if  (phones.moveToFirst()) 
           {  
               do  {  
                   // 遍歷所有的電話號碼   
                   String phoneNumber = phones  
                           .getString(phones  
                                   .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));  
                   int phoneType = phones  
                           .getInt(phones  
                                   .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                   if(phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
                   {
                   samContact.phone = phoneNumber;
                   arrayList.add(samContact);
                   break;
                   }
               } while  (phones.moveToNext()); 
               
           }
           
           
           
}while(cur.moveToNext());
}
//獲取sim卡聯絡人
Uri uri = Uri.parse("content://icc/adn");
Cursor cur2 = getContentResolver().query(  
uri,  
                null ,  
                null ,  
                null ,  
                ContactsContract.Contacts.DISPLAY_NAME  
                        + " COLLATE LOCALIZED ASC" );
System.out.println("contact num from sim card = "+cur2.getCount());
System.out.println("---------------");
if(cur2.moveToFirst())
{
do{
try 
{
SamContact samContact = new SamContact();
samContact.isChoosed = false;
           int  displayNameColumn = cur2.getColumnIndex(People.NAME);
           int  phoneColumn = cur2.getColumnIndex(People.NUMBER);
           samContact.name  = cur2.getString(displayNameColumn);
           if(samContact.name == null)
           {
            continue;
           }
           samContact.phone = cur2.getString(phoneColumn); 
           if(samContact.phone == null)
           {
            continue;
           }
           arrayList.add(samContact);

catch (Exception e) 
{
e.printStackTrace();

}while(cur2.moveToNext());
}


return arrayList;
}


public static class SamContact 
{
public String name = "";
public String phone = "";
public boolean isChoosed = false;

}