导入电话薄

今天将手机升级成姜饼(2.3),审计完,发现电话薄都没了,还好到outlook中找到以前用htc同步的数据。发现,outlook太悲剧了,不能完全导入,最后自己写了个小小小程序,outlook电话薄里直接copy出的格式格式为: 

GPS导航
个人信息:
  地址:

  电话:
  传真:
  移动电话: 13815203***
  网页:
业务信息:
  职务:
  部门:
  办公室:
  公司:
  地址:

  电话:
  传真:
  寻呼机:
  网页:
附注:

通过java代码,转换成 PhoneNumber 对象

package com.lazy.upup.model;
/**
 * <P> [功能] 电话号码</P>
 * <P> [说明] </P>
 * <P> [备注] </P>
 * <P> [环境] android 2.1 以上</P> 
 * @author Lazy
 * @version ver 1.0
 * @2011-4-16 下午08:38:58
 */
public class PhoneNumber {
	
	private int id;			//在data表里的ID
	private int raw;		//在raw里的ID
	private String name1;
	private String name2;
	private String phoneNumber1;
	private String phoneNumber2;
	private String email;
	
	 
	public String getName1() {
		return name1;
	}
	public void setName1(String name1) {
		this.name1 = name1;
	}
	public String getName2() {
		return name2;
	}
	public void setName2(String name2) {
		this.name2 = name2;
	}
	public String getPhoneNumber1() {
		return phoneNumber1;
	}
	public void setPhoneNumber1(String phoneNumber1) {
		this.phoneNumber1 = phoneNumber1;
	}
	public String getPhoneNumber2() {
		return phoneNumber2;
	}
	public void setPhoneNumber2(String phoneNumber2) {
		this.phoneNumber2 = phoneNumber2;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getRaw() {
		return raw;
	}
	public void setRaw(int raw) {
		this.raw = raw;
	}
	
}

 

主要导入的方法代码:

/**
	 * 将电话号码人名写入电话薄
	 * 首先向RawContacts.CONTENT_URI执行一个空值插入,目的是获取系统返回的rawContactId   
     * 这时后面插入data表的依据,只有执行空值插入,才能使插入的联系人在通讯录里面可见  
	 */
	public static void addPeopleNumber(Activity activity ,PhoneNumber pn){
		ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
		int rawContactInsertIndex = ops.size();  
		//首先向RawContacts.CONTENT_URI执行一个空值插入(raw_contacts 表), 为了获取生成的联系人 ID   
        ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)   
                .withValue(RawContacts.ACCOUNT_TYPE, null)   
                .withValue(RawContacts.ACCOUNT_NAME, null)   
                .build());   
        //姓名
        ops.add(ContentProviderOperation.newInsert(android.provider.ContactsContract.Data.CONTENT_URI)   
                .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)   
                .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)   
                .withValue(StructuredName.GIVEN_NAME, pn.getName1())   
                .build());   
        //更新手机号码:Data.RAW_CONTACT_ID 获取上一条语句插入联系人时产生的 ID   
        ops.add(ContentProviderOperation.newInsert(android.provider.ContactsContract.Data.CONTENT_URI)   
                 .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)   
                 .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)   
                 .withValue(Phone.NUMBER, pn.getPhoneNumber1())  // "data1"   
                 .withValue(Phone.TYPE, Phone.TYPE_MOBILE)   
                 .withValue(Phone.LABEL, "手机号1")   
                 .build());  
        //手机号码2
        if(pn.getPhoneNumber2()!=null && !"".equals(pn.getPhoneNumber2().trim())){
	        ops.add(ContentProviderOperation.newInsert(android.provider.ContactsContract.Data.CONTENT_URI)   
	                .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)   
	                .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)   
	                .withValue(Phone.NUMBER, pn.getPhoneNumber2())  // "data1"   
	                .withValue(Phone.TYPE, Phone.TYPE_HOME)
	                .withValue(Phone.LABEL, "手机号2")   
	                .build()); 
        }
        //电子邮件
        if(pn.getEmail()!=null){
	        ops.add(ContentProviderOperation.newInsert(android.provider.ContactsContract.Data.CONTENT_URI)   
	                 .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)   
	                 .withValue(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE)   
	                 .withValue(Email.DATA, pn.getEmail())   
	                 .withValue(Email.TYPE, Email.TYPE_WORK)   
	                 .build());   
        }
        try {
			ContentProviderResult[] results = activity.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
			for(ContentProviderResult result : results){   
				Log.d(Constant.TAG, result.uri.toString());   
	        }   
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (OperationApplicationException e) {
			e.printStackTrace();
		} 
	}
	

 

 

你可能感兴趣的:(android,HTC,mobile)