通用类_获取手机通讯录

获取手机中的通讯录类
 

 

 

private void showContracts() {

		// 建立数据源
		users = new ArrayList<User>();
		
		/* 必须要有android.permission.READ_CONTACTS权限 */
		Cursor cursor = this.getContentResolver().query(android.provider.ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
		   
			while(cursor.moveToNext()){

			String strName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); // 取得联络人的姓名 
			int contactId = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts._ID)); //取得联络人的id
		   	
			Cursor phones = this.getContentResolver().query(android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
					null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null); //查询该联络人的所有电话
			
			int typePhone, resType; //
			String numPhone;
			
			if (phones.getCount() > 0) {
				
				while(phones.moveToNext()){
					
				typePhone = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)); 
				resType = ContactsContract.CommonDataKinds.Phone.getTypeLabelResource(typePhone);//获取电话类型的名称
				
				numPhone = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); //获取电话号码
				
				users.add(new User(strName,  this.getString(resType) + ":" + numPhone));
				}
			}
			
			phones.close();
		}
			cursor.close();
		
		// 建立Adapter并制定数据源
		UserAdapter adapter = new UserAdapter(this, R.layout.user_item,users);

 		// adpater绑定到UI
		mListView.setAdapter(adapter);
	}


 项目地址:http://dl.vmall.com/c008fpjooz

项目环境:Android 2.3.3

项目截图:通用类_获取手机通讯录_第1张图片

 

完成的增删改查(rawContact),在Android保密资料里


 

你可能感兴趣的:(电话)