关闭

读取 联系人

标签: android读取联系人
172人阅读 评论(0) 收藏 举报
分类:


package com.xh.tx.readcontact;

import java.util.ArrayList;
import java.util.List;


import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;


public class MainActivity extends Activity {

	private static final String TAG = "MainActivity";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	
		readContact();
		writeContact();
	}


	private void readContact() {
		
		ContentResolver resolver = this.getContentResolver();
		Cursor cursor = resolver.query(Uri.parse("content://com.android.contacts/raw_contacts"), new String[]{"contact_id"}, null, null, null);
		
		if(null != cursor && cursor.getCount() > 0){
				List<Contact> list = new ArrayList<Contact>();
			while(cursor.moveToNext())
			{
				Integer contact_id = cursor.getInt(0);
				
				Cursor cursor_data = resolver.query(Uri.parse("content://com.android.contacts/data"), new String[]{"mimetype","data1"}, 
						"raw_contact_id = ?", 
						new String[]{String.valueOf(contact_id)},
						null);
				if(null!=cursor_data && cursor_data.getCount()>0){
					
					Contact contact = new Contact();;
					while(cursor_data.moveToNext())
					{
						String mimetype = cursor_data.getString(0);
						String data = cursor_data.getString(1);
					
					if("vnd.android.cursor.item/name".equals(mimetype))
					{
						contact.setName(data);
					}else if("vnd.android.cursor.item/email_v2".equals(mimetype))
					{
						contact.setEmail(data);
					}else if("vnd.android.cursor.item/phone_v2".equals(mimetype))
					{
						contact.getPhone().add(data);
					}else if("vnd.android.cursor.item/postal-address_v2".equals(mimetype))
					{
						contact.setAddress(data);
					}
					
					list.add(contact);
					
					Log.i(TAG, "mimetype=" + mimetype + "  data=" + data );
					
					}
					
				}
			}
			Log.i(TAG, "mimetype="  + "  data="  );
		}
	
	
	}
	
	private void writeContact() {
		//1.获得访问内容提供者的工具类
		ContentResolver resolver = getContentResolver();
		//2. 获取到已存入联系人数据库里面的最后一个联系人的contact_id,然后在加1 就是现在要添加的练习题的contact_id
		Uri raw_contact_uri = Uri.parse("content://com.android.contacts/raw_contacts");
		Cursor cursor = resolver.query(raw_contact_uri, new String[]{"contact_id"}, null, null, "contact_id desc limit 1");
		Integer max_contact_id = 0;
		
		if(null != cursor && cursor.moveToFirst())
		{
			max_contact_id = cursor.getInt(0);
		}
		
		max_contact_id = max_contact_id + 1;
		//3. 往raw_contact表里面添加contact_id
		ContentValues values = new ContentValues();
		values.put("contact_id", max_contact_id);
		
		raw_contact_uri = resolver.insert(raw_contact_uri, values);
		Log.i(TAG, "raw_contact_uri=" + raw_contact_uri);
		//4. 往data里面添加数据(姓名,email,address,phone等信息),注意:一个信息是一样记录,所以添加一个联系人的多个信息的时候要执行多个insert操作
		Uri data_uri = Uri.parse("content://com.android.contacts/data");
		
		values.clear(); //复用之前一定要清空否则会影响下面的插入操作
		values.put("raw_contact_id", max_contact_id);
		values.put("mimetype", "vnd.android.cursor.item/name");
		values.put("data1", "1505");
		resolver.insert(data_uri, values);
		
		values.clear(); //复用之前一定要清空否则会影响下面的插入操作
		values.put("raw_contact_id", max_contact_id);
		values.put("mimetype", "vnd.android.cursor.item/email_v2");
		values.put("data1", "4654654@qq,com");
		resolver.insert(data_uri, values);
		
		values.clear(); //复用之前一定要清空否则会影响下面的插入操作
		values.put("raw_contact_id", max_contact_id);
		values.put("mimetype", "vnd.android.cursor.item/phone_v2");
		values.put("data1", "16824524");
		resolver.insert(data_uri, values);
		
		values.clear(); //复用之前一定要清空否则会影响下面的插入操作
		values.put("raw_contact_id", max_contact_id);
		values.put("mimetype", "vnd.android.cursor.item/postal-address_v2");
		values.put("data1", "西安南路");
		resolver.insert(data_uri, values);
	}
}

package com.xh.tx.readcontact;

import java.util.ArrayList;
import java.util.List;

public class Contact {
	private String name;
	private String email;
	private String address;
	private List<String> phone = new ArrayList<String>();
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public List<String> getPhone() {
		return phone;
	}
	public void setPhone(List<String> phone) {
		this.phone = phone;
	}
	
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:23056次
    • 积分:1136
    • 等级:
    • 排名:千里之外
    • 原创:91篇
    • 转载:20篇
    • 译文:0篇
    • 评论:1条
    文章分类
    文章存档
    最新评论