通用类_获取手机通讯录

获取手机中的通讯录类
 

 

 

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

项目截图:

 

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


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值