当前位置: 代码迷 >> Android >> 获取android手机了通讯录跟sim卡联系人
  详细解决方案

获取android手机了通讯录跟sim卡联系人

热度:16   发布时间:2016-05-01 17:01:34.0
获取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;			}
  相关解决方案