ContentResolver获取手机联系人
博客专区 > Leozhu 的博客 > 博客详情
ContentResolver获取手机联系人
Leozhu 发表于3年前
ContentResolver获取手机联系人
  • 发表于 3年前
  • 阅读 43
  • 收藏 0
  • 点赞 0
  • 评论 0

标题:腾讯云 新注册用户域名抢购1元起>>>   

摘要: 快速获取联系人

android系统的联系人是通过ContentProvider来提供的,想要获取联系人可使用ContentResolver。不啰嗦上代码

public class MainActivity extends Activity {
//用来展示联系人
ListView listview = null;
//展示联系人的适配器
MylistAdapter myadapter = null;

//存放联系人名称的listview
ArrayList<String> nameList = new ArrayList<String>();
//存放联系人号码的listview	
ArrayList<String> numberList = new ArrayList<String>();
//存放联系人头像的列表	
ArrayList<Bitmap> headList = new ArrayList<Bitmap>();

/** 获取库Phon表字段 **/
private static final String[] PHONES_PROJECTION = new String[] {
		Phone.DISPLAY_NAME, Phone.NUMBER, Photo.PHOTO_ID, Phone.CONTACT_ID };

/** 联系人显示名称 **/
private static final int PHONES_DISPLAY_NAME_INDEX = 0;

/** 电话号码 **/
private static final int PHONES_NUMBER_INDEX = 1;

/** 头像ID **/
private static final int PHONES_PHOTO_ID_INDEX = 2;

/** 联系人的ID **/
private static final int PHONES_CONTACT_ID_INDEX = 3;

/** 联系人名称 **/
private ArrayList<String> mContactsName = new ArrayList<String>();

/** 联系人号码 **/
private ArrayList<String> mContactsNumber = new ArrayList<String>();

/** 联系人头像 **/
private ArrayList<Bitmap> mContactsPhonto = new ArrayList<Bitmap>();

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
	listview = (ListView) findViewById(R.id.listview);

	getNumber();

	MylistAdapter adapter = new MylistAdapter();

	View view = null;
	view = view.inflate(MainActivity.this, R.layout.contactcount, null);
	TextView textCount = (TextView) view.findViewById(R.id.count);
	textCount.setText(nameList.size() + "位联系人");
	//listview底部添加底部标题
            listview.addFooterView(view);

	listview.setAdapter(adapter);

}

//获取联系人方法
public void getNumber() {

//获取ContentResolver实例
	ContentResolver resolver = MainActivity.this.getContentResolver();

//使用ContentResolver通过uri和获取字段
	Cursor phoneCursor = resolver.query(Phone.CONTENT_URI,
			PHONES_PROJECTION, null, null, null);

	if (phoneCursor != null) {
		while (phoneCursor.moveToNext()) {

//获取联系人号码 String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX); //获取联系人姓名 String contactName = phoneCursor .getString(PHONES_DISPLAY_NAME_INDEX); //获取联系人ID Long contactId = phoneCursor.getLong(PHONES_CONTACT_ID_INDEX); //获取联系人头像ID Long photoId = phoneCursor.getLong(PHONES_PHOTO_ID_INDEX);

			// 得到联系人头像Bitamp
			Bitmap contactPhoto = null;

			// photoid 大于0 表示联系人有头像 如果没有给此人设置头像则给他一个默认的
			if (photoId > 0) {
				Uri uri = ContentUris.withAppendedId(
						ContactsContract.Contacts.CONTENT_URI, contactId);
				InputStream input = ContactsContract.Contacts
						.openContactPhotoInputStream(resolver, uri);
				contactPhoto = BitmapFactory.decodeStream(input);
			} else {
				contactPhoto = BitmapFactory.decodeResource(getResources(),
						R.drawable.head);
			}

			nameList.add(contactName);
			numberList.add(phoneNumber);
			headList.add(contactPhoto);

		}
	}
}
}


class MylistAdapter extends BaseAdapter {
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return nameList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		convertView = convertView.inflate(MainActivity.this,
				R.layout.contactitem, null);

		ImageView head = (ImageView) convertView.findViewById(R.id.head);
		TextView name = (TextView) convertView.findViewById(R.id.name);
		TextView num = (TextView) convertView.findViewById(R.id.num);

		head.setImageBitmap(headList.get(position));
		name.setText(nameList.get(position));
		num.setText(numberList.get(position));

		return convertView;
	}
}

布局见下方,至于图片随便弄个放在drawable文件中,manifest文件注意添加读取联系人权限

主界面布局 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" >

<ListView
    android:id="@+id/listview"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</ListView>

</RelativeLayout>

列表条目布局 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">

<ImageView
    android:id="@+id/head"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="10dp"
    android:layout_width="20dp"
    android:layout_height="20dp" />

<TextView
    android:id="@+id/name"
    android:layout_centerHorizontal="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/num"
    android:layout_alignParentRight="true"
    android:layout_marginRight="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</RelativeLayout>

列表底部布局 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >

<TextView android:id="@+id/count"
    android:layout_centerInParent="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

</RelativeLayout>
共有 人打赏支持
粉丝 0
博文 4
码字总数 791
×
Leozhu
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: