`
samyou
  • 浏览: 108006 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

获取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;
		
	}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics