android获取联系人所有内容

public class GetContactsInfo {
	List<ContactsInfo> localList;
	List<ContactsInfo> SIMList;
	Context context;
	ContactsInfo contactsInfo;
	ContactsInfo SIMContactsInfo;

	public GetContactsInfo(Context context) {
		localList = new ArrayList<ContactsInfo>();
		SIMList = new ArrayList<ContactsInfo>();
		this.context = context;

	}

	// ----------------得到本地联系人信息-------------------------------------
	public List<ContactsInfo> getLocalContactsInfos() {
		ContentResolver cr = context.getContentResolver();
		String str[] = { Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER,
				Phone.PHOTO_ID };
		Cursor cur = cr.query(
				ContactsContract.CommonDataKinds.Phone.CONTENT_URI, str, null,
				null, null);

		if (cur != null) {
			while (cur.moveToNext()) {
				contactsInfo = new ContactsInfo();
				contactsInfo.setContactsPhone(cur.getString(cur
						.getColumnIndex(Phone.NUMBER)));// 得到手机号码
				contactsInfo.setContactsName(cur.getString(cur
						.getColumnIndex(Phone.DISPLAY_NAME)));
				// contactsInfo.setContactsPhotoId(cur.getLong(cur.getColumnIndex(Phone.PHOTO_ID)));
				long contactid = cur.getLong(cur
						.getColumnIndex(Phone.CONTACT_ID));
				long photoid = cur.getLong(cur.getColumnIndex(Phone.PHOTO_ID));
				// 如果photoid 大于0 表示联系人有头像 ,如果没有给此人设置头像则给他一个默认的
				if (photoid > 0) {
					Uri uri = ContentUris.withAppendedId(
							ContactsContract.Contacts.CONTENT_URI, contactid);
					InputStream input = ContactsContract.Contacts
							.openContactPhotoInputStream(cr, uri);
					contactsInfo.setBitmap(BitmapFactory.decodeStream(input));
				} else {
					contactsInfo.setBitmap(BitmapFactory.decodeResource(
							context.getResources(), R.drawable.ic_launcher));
				}

				System.out.println("---------联系人电话--"
						+ contactsInfo.getContactsPhone());
				localList.add(contactsInfo);

			}
		}
		cur.close();
		return localList;

	}

	public List<ContactsInfo> getSIMContactsInfos() {
		TelephonyManager mTelephonyManager = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		
              System.out.println("---------SIM--------");
			ContentResolver cr = context.getContentResolver();
			final String SIM_URI_ADN = "content://icc/adn";// SIM卡
			

				Uri uri = Uri.parse(SIM_URI_ADN);
				Cursor cursor = cr.query(uri, null, null, null, null);
				while (cursor.moveToFirst()) {
						SIMContactsInfo = new ContactsInfo();
						SIMContactsInfo.setContactsName(cursor.getString(cursor
								.getColumnIndex("name")));
						SIMContactsInfo
								.setContactsPhone(cursor.getString(cursor
										.getColumnIndex("number")));
						SIMContactsInfo
								.setBitmap(BitmapFactory.decodeResource(
										context.getResources(),
										R.drawable.ic_launcher));
						SIMList.add(SIMContactsInfo);
				}
				cursor.close();
				
				
			

		
		return SIMList;
	}
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值