码迷,mamicode.com
首页 > 移动开发 > 详细

Android通讯录管理一之联系人获取

时间:2015-04-01 17:45:12      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:android   联系人   通讯   

正如我们知道的一样,Android的通讯录和短信管理是通过contentprovider来向开发者来开发接口的。必须从ContentResolver入手去解决。其中通讯录操作涉及到系统源码api的使用,特别是在表的uri上面容易弄混。在接下来的几篇文章中蜗牛将陆续为大家推出Android通讯管理相关知识的文章。其中包括联系人获取、通话记录获取、短信获取、短信详情获取发送短信。废话不多说先上图

先看看联系人的表的结构

技术分享

其中对于开发这来说主要关注以上三个表,其中要用到联合查询,关于三张表的设计可以百度到相关文章。

代码最终效果图

技术分享

联系人:

技术分享

想要源码的童鞋可以在下载http://download.csdn.net/detail/waniu123/8554533;

package cn.zxw.contact.domain;

/**
 * 联系人
 * @author zhan
 *
 */
public class ContactsInfo {
	public int _id;
	public String name;
	public String phone;
	public String email;
	public ContactsInfo() {
		super();
	}

	public ContactsInfo(int _id, String name, String phone, String email) {
		super();
		this._id = _id;
		this.name = name;
		this.phone = phone;
		this.email = email;
	}

	@Override
	public String toString() {
		return "PersonInfo [_id=" + _id + ", name=" + name + ", phone=" + phone
				+ ", email=" + email + "]";
	}

}
获取联系人
/**
	 * 获取联系人
	 * 
	 * @param context
	 * @return
	 */
	public static List<ContactsInfo> getContactsInfos(Context context) {
		ContentResolver resolver = context.getContentResolver();
		List<ContactsInfo> infos = new ArrayList<ContactsInfo>();
		// 获取联系人数据 访问联系人的内容提供者
		// ContactsContract.AUTHORITY com.android.contacts 授权
		// 该内容提供者操作是需要读写权限
		// matcher.addURI(ContactsContract.AUTHORITY, "raw_contacts",
		// RAW_CONTACTS);
		// matcher.addURI(ContactsContract.AUTHORITY, "raw_contacts/#/data",
		// RAW_CONTACTS_DATA);
		Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
		Cursor cursor1 = resolver.query(uri, new String[] { "_id" }, null,
				null, null);
		while (cursor1.moveToNext()) {
			int _id = cursor1.getInt(0);
			ContactsInfo info = new ContactsInfo();
			uri = Uri.parse("content://com.android.contacts/raw_contacts/"
					+ _id + "/data");
			Cursor cursor2 = resolver.query(uri, new String[] { "data1",
					"mimetype" }, null, null, null);
			while (cursor2.moveToNext()) {
				String data1 = cursor2.getString(0);
				String mimetype = cursor2.getString(1);
				if ("vnd.android.cursor.item/phone_v2".equals(mimetype)) {// 号码
					info.phone = data1;
				} else if ("vnd.android.cursor.item/name".equals(mimetype)) {// 姓名
					info.name = data1;
				}
			}

			cursor2.close();
			infos.add(info);
		}
		cursor1.close();

		return infos;
	}

	/**
	 * 获取所有的通话记录
	 * 
	 * @param context
	 * @return
	 */
	public  List<CallLogInfo> getCallLog(Context context) {
		List<CallLogInfo> infos = new ArrayList<CallLogInfo>();
		ContentResolver cr = context.getContentResolver();
		Uri uri = Calls.CONTENT_URI;
		String[] projection = new String[] { Calls.NUMBER, Calls.DATE,
				Calls.TYPE };
		Cursor cursor = cr.query(uri, projection, null, null, null);
		while (cursor.moveToNext()) {
			String number = cursor.getString(0);
			long date = cursor.getLong(1);
			int type = cursor.getInt(2);
			infos.add(new CallLogInfo(number, date, type));
		}
		cursor.close();
		return infos;
	}

package cn.zxw.contact;

import java.util.ArrayList;

import java.util.List;

import cn.zxw.contact.domain.ContactsInfo;
import cn.zxw.contact.utils.ContactsMsgUtils;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import android.app.Activity;

/**
 * 联系人
 * 
 * @author zhan
 * 
 */

public class ContactsActivity extends Activity {

	private ListView lv;

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

		List<ContactsInfo> infos = ContactsMsgUtils
				.getContactsInfos(getApplicationContext());
		MyAdapter adapter = new MyAdapter(infos);
		lv.setAdapter(adapter);
	}

	private class MyAdapter extends BaseAdapter {
		//
		List<ContactsInfo> infos = new ArrayList<ContactsInfo>();
		private TextView tv_number;
		private TextView tv_name;

		public MyAdapter(List<ContactsInfo> infos) {
			super();
			this.infos = infos;
		}

		@Override
		public int getCount() {
			return infos.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = View.inflate(getApplicationContext(),
					R.layout.contacts_list_item, null);
			tv_number = (TextView) view.findViewById(R.id.tv_number);
			tv_name = (TextView) view.findViewById(R.id.tv_name);
			ContactsInfo info = infos.get(position);
			tv_number.setText(info.phone);
			tv_name.setText(info.name);
			return view;
		}

	}
}










Android通讯录管理一之联系人获取

标签:android   联系人   通讯   

原文地址:http://blog.csdn.net/waniu123/article/details/44808859

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!