`
mjbb
  • 浏览: 86570 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Contact 联系人工具类(一)

阅读更多
从数据库中取出数据封装成对象:

package com.litsoft.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContacts;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.Im;
import android.provider.ContactsContract.CommonDataKinds.Nickname;
import android.provider.ContactsContract.CommonDataKinds.Note;
import android.provider.ContactsContract.CommonDataKinds.Organization;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.Photo;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
import android.provider.ContactsContract.CommonDataKinds.Website;

import com.litsoft.domain.Contact2;

public class DB2ContactUtil {
	public static ContentResolver resolver;
	public static String numberSplit="NO";
	public static String emailSplit=";";
	/**
	 * 删除一个
	 */
	public static void  deleteById(String contactId ){
		 resolver.delete(Data.CONTENT_URI, ContactsContract.Data.CONTACT_ID + "=" + contactId, null);
		 resolver.delete(RawContacts.CONTENT_URI, RawContacts.CONTACT_ID + "=" + contactId, null);
	}
	/**
	 * 删除所有的 Contact
	 */
	public static void deleteAll(){
		 resolver.delete(Data.CONTENT_URI, null, null);
		 resolver.delete(RawContacts.CONTENT_URI, null, null);
	}
	/**
	 * 获得所有的Contact
	 * @return
	 */
	public static List<Contact2> getAllContact(){
		List<Contact2> contactList = new ArrayList<Contact2>();
		ArrayList<String> idList = getContactIdList();
		Contact2 newContact ;
		for(String contactId:idList){
			newContact = new Contact2();
			newContact.setId(contactId);
			//1.头像
			newContact.setContactIcon(getContactIcon(contactId));
			//2.名字
			newContact.setName(getName(contactId));
			//3.电话
			newContact.setPhoneNumberMap(getPhoneNumber(contactId));
			//4.Email
			newContact.setEmailMap(getEmail(contactId));
			//5.im
			newContact.setImMap(getAIM(contactId));
			//6.add
			newContact.setAddrMap(getAddressDetail(contactId));
			//7.organization
			newContact.setOrganizationMap(getOrganizations(contactId));
			//8.notes
			newContact.setNotes(getNotes(contactId));
			//9.nickname
			newContact.setNicks(getNick(contactId));
			//10.website
			newContact.setWebsites(getWebsite(contactId));
			
			contactList.add(newContact);
		}
		return contactList;
	}
	/**
	 * 根据id 获得 头像
	 * @param contactId
	 * @return
	 */
	public static byte[] getContactIcon(String contactId){
		byte[] image = null;
		Cursor cursor = resolver.query(Data.CONTENT_URI, null,
				Photo.CONTACT_ID + " = " + contactId + " and " + Data.MIMETYPE
				+ " = '" + Photo.CONTENT_ITEM_TYPE+"'", null, null);
		if(cursor.moveToNext()){
			image = cursor.getBlob(cursor.getColumnIndex(Photo.PHOTO));
		}
		return image;
	}
	/**
	 * 根据Id 得到 familyName 和 giveName [1,0]
	 */
	public static  String[] getName(String contactId){
		String name[] = new String[2];
		Cursor cursor = resolver.query(Data.CONTENT_URI, null,
				StructuredName.CONTACT_ID + " = " + contactId + " and " + Data.MIMETYPE
				+ " = '" + StructuredName.CONTENT_ITEM_TYPE+"'", null, null);
		while(cursor.moveToNext()){
			name[1]= cursor.getString(cursor.getColumnIndex(StructuredName.FAMILY_NAME));
			name[0] = cursor.getString(cursor.getColumnIndex(StructuredName.GIVEN_NAME));	
		}
		System.out.println("give name :" + name[0] + " famliy name :" + name[1]);
		return name;
	}
	
	/**
	 * 获得所有联系人的id列表
	 */
	public static ArrayList<String> getContactIdList() {
		ArrayList<String> contactIdList = new ArrayList<String>();
		Cursor cursor = resolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
		String id = "";
		while (cursor.moveToNext()) {
			id = cursor.getString(cursor.getColumnIndex(Contacts._ID));
			contactIdList.add(id);
			System.out.println("contractId is :"+id);
		}
		return contactIdList;
	}
	
	/**
	 * 根据用户Id 电话号码
	 * 
	 * @param contactId key{1,2,3,7} ==={home,mobile,work,other}
	 * @return
	 */
	public static Map<String,String> getPhoneNumber(String contactId) {
		Map<String,String> phoneNumberMap = new HashMap<String, String>();
		String value = "";
		String key ="";
		Cursor phones = resolver.query(Phone.CONTENT_URI, null,
				Phone.CONTACT_ID + " = " + contactId, null, null);
		while (phones.moveToNext()) {
			key = phones.getString(phones.getColumnIndex(Phone.DATA2));
			value = phones.getString(phones.getColumnIndex(Phone.DATA1));
			if("7".equals(key)){
				String v = phoneNumberMap.get(key);
				if(v!=null&&!"".equals(v)){
					value = v+numberSplit+ value;
				}
			}
			phoneNumberMap.put(key, value);
			System.out.println("number type is :" + key + " number is :" + value);
		}
		
		return phoneNumberMap;
	}
	
	/**
	 * 根据用户Id  获得email 
	 * key {1,2,3,4} =={home,work,other, mobile }
	 * 
	 */
	public static Map<String,String> getEmail(String contactId) {
		Map<String,String> emailMap = new HashMap<String, String>();
		String key = "";
		String value = "";
		Cursor emails = resolver.query(Email.CONTENT_URI, null,
				Email.CONTACT_ID + " = " + contactId, null, null);
		while (emails.moveToNext()) {
			
			key = emails.getString(emails.getColumnIndex(Email.DATA2));
			value = emails.getString(emails.getColumnIndex(Email.DATA1));
			if("4".equals(key)){
				String v = emailMap.get(key);
				if(v!=null&&!"".equals(v)){
					value = v+emailSplit+ value;
				}
			}
			emailMap.put(key, value);
			System.out.println("email type is :" + key + " email is :" + value);
		}
		return emailMap;
	}
	/**
	 * 根据用户Id获得 AIM
	 * key {0,1,2,3,4,5,6,7} == {aim,live,yahoo,skyp,qq,gtalk,icq,jabber}
	 * @param contactId 
	 * @return 
	 */
	public static Map<String,String> getAIM(String contactId) {
		Map<String,String> im =new HashMap<String,String>();
		String key;
		String value;
		String selection = Im.CONTACT_ID + " = " + contactId //获得联系人的具体id    
		+ " and " + Data.MIMETYPE + " = '" + CommonDataKinds.Im.CONTENT_ITEM_TYPE+"'";
		Cursor cursor = resolver.query(Data.CONTENT_URI, null, selection, null, null);
		while(cursor.moveToNext()){
			key = cursor.getString(cursor.getColumnIndex(Im.DATA5));//判断im 不同种类
			value = cursor.getString(cursor.getColumnIndex(Im.DATA1));
			if("7".equals(key)){//jabber类型
				String v = im.get(key);
				if(v!=null&&!"".equals(v)){
					value = v+";"+ value;
				}
				
			}
			im.put(key, value);
			System.out.println("aimType :"+ key +" ; aim : " + value);
		}
		//System.out.println("aim : " + aim);
		return im;
	}

	//=======================================Map<String,List<String[]>>类型========================================
	/**
	 * 根据用户Id 获得 Address
	 * data4-data10 : {street pobox neigborbood city state zip country}==str[0-6]
	 * key {1,2,3} =={home,work,other}
	 */
	public static Map<String,List<String[]>> getAddressDetail(String contactId) {
		Map<String,List<String[]>> postalMap = new HashMap<String, List<String[]>>();
		String key;
		List<String[]> addrList;
		String addr[];
		Cursor adds = resolver
				.query(StructuredPostal.CONTENT_URI, null,
						StructuredPostal.CONTACT_ID + " = " + contactId, null, null);
		while (adds.moveToNext()) {
			key  = adds.getString(adds
					.getColumnIndex(StructuredPostal.DATA2));
			addr = new String[7];
			addr[0] = adds.getString(adds
					.getColumnIndex(StructuredPostal.STREET));
			addr[1] =adds.getString(adds
					.getColumnIndex(StructuredPostal.POBOX));//信箱号码
			addr[2] = adds.getString(adds
					.getColumnIndex(StructuredPostal.NEIGHBORHOOD));
			addr[3] = adds.getString(adds
					.getColumnIndex(StructuredPostal.CITY));
			addr[4]=adds.getString(adds
					.getColumnIndex(StructuredPostal.REGION));// 省份
			addr[5] =adds.getString(adds
					.getColumnIndex(StructuredPostal.POSTCODE));// 邮政编码
			addr[6] = adds.getString(adds
					.getColumnIndex(StructuredPostal.COUNTRY));
			addrList = postalMap.get(key);
			if(addrList!=null&&!addrList.isEmpty()){
				addrList.add(addr);
				postalMap.put(key, addrList);
			}else{
				addrList = new ArrayList<String[]>();	
				addrList.add(addr);
				postalMap.put(key, addrList);
			}
			System.out.println("address's type is :"+ key +" -- address: " + addr[0]+":"+ addr[1]+":"+addr[2]+":"+addr[3]+":"+addr[4]+":"+addr[5]+":"+addr[6]);
		}
		
		return postalMap;
	}
	/**
	 * 根据用户Id 获得 公司
	 * organization: Map<String,List> String 是数据类型  List 是一个数组:String [data1(company),data4(position)]
	 * key {1,2} ==={work ,other}
	 */
	public static Map<String,List<String[]>> getOrganizations(String contactId) {
		Map<String,List<String[]>> companyMap = new HashMap<String, List<String[]>>();
		String key = "";
		List<String[]> positionList;
		String pos[];
		
		Cursor organization = resolver.query(Data.CONTENT_URI, null,
				Data.CONTACT_ID + "=" + contactId + " AND " + Data.MIMETYPE
						+ "='" + Organization.CONTENT_ITEM_TYPE + "'", null,
				null);
		while (organization.moveToNext()) {
			pos =  new String[2];
			key = organization.getString(organization
					.getColumnIndex(CommonDataKinds.Organization.DATA2));
			pos[0] = organization.getString(organization
					.getColumnIndex(CommonDataKinds.Organization.COMPANY));
			pos[1] = organization.getString(organization
					.getColumnIndex(CommonDataKinds.Organization.TITLE));
			positionList = companyMap.get(key);
			if(positionList!=null&&!positionList.isEmpty()){
				positionList.add(pos);
				companyMap.put(key, positionList);
			}else{
				positionList = new ArrayList<String[]>();	
				positionList.add(pos);
				companyMap.put(key, positionList);
			}
//			positionList.add(pos);
			System.out.println("--------------------------------");
			System.out.println("type:" + key);
			System.out.println("company:" + pos[0]);
			System.out.println("position:" + pos[1]);
			
		}
		
			return companyMap;
		
	}
	//=========================================List<String> 数据类型===========================================
	/**
	 * 根据用户id 获得notes
	 */
	public static List<String> getNotes(String contactId) {
		List<String> notesList = new ArrayList<String>();
		String note = "";
		Cursor notes = resolver.query(Data.CONTENT_URI, null,
				Data.CONTACT_ID + "=" + contactId + " AND " + Data.MIMETYPE
						+ "='" + Note.CONTENT_ITEM_TYPE + "'",
				null, null);
		while (notes.moveToNext()) {
			note = notes
					.getString(notes
							.getColumnIndex(Note.NOTE));
			System.out.println("note :" + note);
			notesList.add(note);
		}
		return notesList;
	}
	/**
	 * 根据用户id 获得nicks
	 */
	public static List<String> getNick(String contactId) {
		List<String> nickList = new ArrayList<String>();
		String nick = "";
		Cursor nicks = resolver.query(Data.CONTENT_URI, null,
				Data.CONTACT_ID + "=" + contactId + " AND " + Data.MIMETYPE
						+ "='" + Nickname.CONTENT_ITEM_TYPE + "'",
				null, null);
		while (nicks.moveToNext()) {
			nick = nicks
					.getString(nicks
							.getColumnIndex(Nickname.DATA1));
			System.out.println("nick :" + nick);
			nickList.add(nick);
		}
		return nickList;
	}
	/**
	 * 根据用户id 获得website
	 */
	public static List<String> getWebsite(String contactId) {
		List<String> webSiteList = new ArrayList<String>();
		String webSite = "";
		Cursor nicks = resolver.query(Data.CONTENT_URI, null,
				Data.CONTACT_ID + "=" + contactId + " AND " + Data.MIMETYPE
						+ "='" + Website.CONTENT_ITEM_TYPE + "'",
				null, null);
		while (nicks.moveToNext()) {
			webSite = nicks
					.getString(nicks
							.getColumnIndex(Website.DATA1));
			webSiteList.add(webSite);
			System.out.println("webSite : "+ webSite);
		}
		return webSiteList;
	}
}

分享到:
评论
2 楼 greenboy1 2010-12-06  
非常好 谢谢
1 楼 bug_shi 2010-11-20  
import com.litsoft.domain.Contact2; 在那里啊,小的刚入门请指教。

相关推荐

    在线电话簿web应用程序设计开发.zip

    1.3、联系人检索1.3.1 根据联系人姓名模糊查询1.3.2 根据联系人性别查询1.3.3 根据联系人所属分组查询1.4、联系人管理1.4.1 联系人更新可更新信息:联系人移动电话、联系人固定电话、联系人电子邮箱、联 系人 QQ、...

    Android保存联系人到通讯录的方法

    因为是一个工具类,所以我这里就只给一个方法了,也是很简单,但是写的没有读取联系人的数据那么多,要保存更多其实看下如何读取的就会了。 直接上源码:  /** * 添加联系人到本机 * * @param context * @...

    Point de Contact-crx插件

    Point de Contact提供此扩展,使您可以轻松匿名地报告任何令人反感的内容。 在采取任何行动之前,Point de Contact团队将继续对所发布内容的法律资格进行审查,并将其整合到其汇款中。 根据联系点处理内容的合法分类...

    新版Android开发教程.rar

    Android 进一步推进了 " 随时随地为每个人提供信息 " 这一企 业 目标的实现。 � Open Handset Alliance 汇集了多家业界巨头。运营商如: China Mobile 、 NTT DoCoMo 、 Vodafone 、 T-M obile 等;设备制造商如 ...

    JAVA上百实例源码以及开源项目

     Java二进制IO类与文件复制操作实例,好像是一本书的例子,源代码有的是独立运行的,与同目录下的其它代码文件互不联系,这些代码面向初级、中级Java程序员。 Java访问权限控制源代码 1个目标文件 摘要:Java源码,...

    ContactContentProvider

    自己写的关于系统联系人的增删改查的方法,并封装了工具类,比较全,分享给大家

    图像元数据库和工具-C/C++开发

    Travis AppVeyor GitLab Codecov副本矩阵聊天目录欢迎使用Exiv2 Contact Building,安装i Travis AppVeyor GitLab Codecov副本矩阵聊天目录欢迎使用Exiv2联系人构建,目录,欢迎使用Exiv2 Contact在Exif2 Contact上...

    java开源包1

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    java开源包11

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    java开源包2

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    java开源包3

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    java开源包6

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    java开源包5

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    java开源包10

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    java开源包4

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    java开源包8

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    java开源包7

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    java开源包9

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    JAVA上百实例源码以及开源项目源代码

     Java二进制IO类与文件复制操作实例,好像是一本书的例子,源代码有的是独立运行的,与同目录下的其它代码文件互不联系,这些代码面向初级、中级Java程序员。 Java访问权限控制源代码 1个目标文件 摘要:Java源码,...

Global site tag (gtag.js) - Google Analytics