获取手机通讯录

在公司里写了一段上传用户通讯录的代码。写下来,以便以后查阅;
获取手机里的通讯录:

/**
	 * 获取通讯录
	 * @param context
	 * @return 返回包含用户名,与号码的ArrayList
	 */
	public ArrayList<UserAddressBook> getContact(Context context) {
		ArrayList<UserAddressBook> allAddress = new ArrayList<UserAddressBook>();
		Cursor cursor = context.getContentResolver().query(
				ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
		while (cursor.moveToNext()) {
			String phoneName = cursor.getString(cursor
					.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
			Logs.v("name", "-->  "+phoneName);
			
			UserAddressBook addressBook = new UserAddressBook();
			addressBook.setItem(phoneName);
			
			String contactId = cursor.getString(cursor
					.getColumnIndex(ContactsContract.Contacts._ID));
			String hasPhone = cursor.getString(cursor
							.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

			if (hasPhone.compareTo("1") == 0) {
				Cursor phones = context.getContentResolver().query(
						ContactsContract.CommonDataKinds.Phone.CONTENT_URI,	null,
						ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = " + contactId, null, null);
				while (phones.moveToNext()) { 
					String phoneNumber = phones
							.getString(phones
									.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
					addressBook.setPhoneNum(phoneNumber);
					Logs.e(TAG,  "   -->  " + phoneNumber);
				}
				phones.close();
			}
			allAddress.add(addressBook);
		}
		cursor.close();
		return allAddress;
	}

用类的方式封装数据:

public class UserAddressBook {
	private String item = null;				//姓名
	private String phoneNum = null;        //电话号码
	
	/**
	 * 姓名
	 * @return
	 */
	public String getItem() {
		return item;
	}
	
	/**
	 * 姓名
	 * @return
	 */
	public void setItem(String item) {
		this.item = item;
	}
	
	/**
	 * 号码
	 * @return
	 */
	public String getPhoneNum() {
		return phoneNum;
	}
	
	/**
	 * 号码
	 * @return
	 */
	public void setPhoneNum(String phoneNum) {
		this.phoneNum = phoneNum;
	}

后记:

 自己作为一个程序员,虽然是测试,看到自己手机号被上传到服务器上时,内心里还是紧张了一会。不知道的情况下,没多少感觉。在自己清楚知道数据被上传并且是自己熟悉的人知道的时候,有种被偷窥的感觉,真的很难受;

还没有想到一种可以防止通讯录被获取的情况;得找找。因为自己的手机已经root了,已没有安全性可言。但自己还是得注意。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值