获取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;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值