Android 联系人开发- 保存联系人

fanleiym 2010-03-02

最近在开发android平台的联系人部分,有点总结和大家分享一下:

参数:

String name,   联系人姓名

String homePhone,  家庭电话

String mobile,   手机

String workphone,  公司电话

String workfax,       公司传真

String emailAddr,    邮件地址

String msn,        String skype,

String qq,

String company,      公司名称

String position,       职位

String homeAddr,   家庭地址

String companyAddr  公司地址

android在保存联系人的时候要特别注意的是 电话和手机的输入格式,以及添加IM时候要主要private属性

1) 手机格式: 1-390-123-1122, 经过验证1开头的号码都被认为按照手机格式显示

2)   电话及传真格式:010-123-11111,经过验证非1开头的号码都被认为按照电话格式显示

3) 添加IM时要加上 values.put(ContactMethods.ISPRIMARY, 0);

下面看看我写的代码吧:

ContentValues values = new ContentValues();

values.put(People.NAME,name);

values.put(Contacts.People.STARRED, 0);//很重要

Uri newPerson = Contacts.People.createPersonInMyContactsGroup(getContentResolver(), values);

Uri numberUri = null;

if(!ifEmpty(homePhone)){

values.clear();

numberUri=Uri.withAppendedPath(newPerson,People.Phones.CONTENT_DIRECTORY);

values.put(Contacts.Phones.TYPE,People.Phones.TYPE_HOME);

values.put(People.NUMBER,homePhone);

getContentResolver().insert(numberUri,values);

    }

    if (!ifEmpty(mobile)) {

values.clear();

numberUri=Uri.withAppendedPath(newPerson,People.Phones.CONTENT_DIRECTORY);

values.put(Contacts.Phones.TYPE,People.Phones.TYPE_MOBILE);

values.put(People.NUMBER,mobile);

     getContentResolver().insert(numberUri, values);

    }

    if (!ifEmpty(workphone)) {

values.clear();

numberUri=Uri.withAppendedPath(newPerson,People.Phones.CONTENT_DIRECTORY);

values.put(Contacts.Phones.TYPE,People.Phones.TYPE_WORK);

values.put(People.NUMBER,workphone);

getContentResolver().insert(numberUri,values);

    }

    if (!ifEmpty(workfax)) {

values.clear();

numberUri=Uri.withAppendedPath(newPerson,People.Phones.CONTENT_DIRECTORY);

values.put(Contacts.Phones.TYPE,People.Phones.TYPE_FAX_WORK);

values.put(People.NUMBER,workfax);

getContentResolver().insert(numberUri,values);

    }

    if (!ifEmpty(emailAddr)) {

values.clear();

numberUri=Uri.withAppendedPath(newPerson,People.ContactMethods.CONTENT_DIRECTORY);

values.put(Contacts.ContactMethods.KIND,Contacts.KIND_EMAIL);

values.put(Contacts.ContactMethods.DATA,emailAddr);

values.put(Contacts.ContactMethods.TYPE,Contacts.ContactMethods.TYPE_HOME);

getContentResolver().insert(numberUri,values);

    }

    //add IM tools

if(!ifEmpty(msn)){

values.clear();

numberUri=Uri.withAppendedPath(newPerson,People.ContactMethods.CONTENT_DIRECTORY);

values.put(Contacts.ContactMethods.KIND,Contacts.KIND_IM);

values.put(ContactMethods.TYPE,ContactMethods.TYPE_OTHER);

     values.put(Contacts.ContactMethods.DATA, msn);

     values.put(ContactMethods.AUX_DATA, ContactMethods.encodePredefinedImProtocol(ContactMethods.PROTOCOL_MSN));

values.put(ContactMethods.ISPRIMARY,0);//很重要,否则添加时出错

getContentResolver().insert(numberUri,values);

    }

    if (!ifEmpty(skype)) {

values.clear();

numberUri=Uri.withAppendedPath(newPerson,People.ContactMethods.CONTENT_DIRECTORY);

values.put(Contacts.ContactMethods.KIND,Contacts.KIND_IM);

values.put(ContactMethods.TYPE,ContactMethods.TYPE_OTHER);

     values.put(Contacts.ContactMethods.DATA, skype);

     values.put(ContactMethods.AUX_DATA, ContactMethods.encodePredefinedImProtocol(ContactMethods.PROTOCOL_SKYPE));

values.put(ContactMethods.ISPRIMARY,0);//很重要,否则添加时出错

getContentResolver().insert(numberUri,values);

    }

    if (!ifEmpty(qq)) {

values.clear();

numberUri=Uri.withAppendedPath(newPerson,People.ContactMethods.CONTENT_DIRECTORY);

values.put(ContactMethods.KIND,Contacts.KIND_IM);

values.put(ContactMethods.TYPE,ContactMethods.TYPE_OTHER);

     values.put(Contacts.ContactMethods.DATA, qq);

     values.put(ContactMethods.AUX_DATA, ContactMethods.encodePredefinedImProtocol(ContactMethods.PROTOCOL_QQ));     values.put(ContactMethods.ISPRIMARY, 0); //很重要,否则添加时出错     

     getContentResolver().insert(numberUri, values);    }

    // add company and title

if(!ifEmpty(company)||!ifEmpty(position)){

values.clear();

numberUri=Uri.withAppendedPath(newPerson,Contacts.Organizations.CONTENT_DIRECTORY);

if(!ifEmpty(company)){

values.put(Contacts.Organizations.COMPANY,company);

}

if(!ifEmpty(position)){

values.put(Contacts.Organizations.TITLE,position);

}

values.put(Contacts.Organizations.TYPE,Contacts.Organizations.TYPE_WORK);

getContentResolver().insert(numberUri,values);

    }

    //add address

if(!ifEmpty(homeAddr)){

values.clear();

numberUri=Uri.withAppendedPath(newPerson,People.ContactMethods.CONTENT_DIRECTORY);

values.put(Contacts.ContactMethods.KIND,Contacts.KIND_POSTAL);

values.put(Contacts.ContactMethods.TYPE,Contacts.ContactMethods.TYPE_HOME);

values.put(Contacts.ContactMethods.DATA,homeAddr);

getContentResolver().insert(numberUri,values);

    }

    if (!ifEmpty(companyAddr)) {

values.clear();

numberUri=Uri.withAppendedPath(newPerson,People.ContactMethods.CONTENT_DIRECTORY);

values.put(Contacts.ContactMethods.KIND,Contacts.KIND_POSTAL);

values.put(Contacts.ContactMethods.TYPE,Contacts.ContactMethods.TYPE_WORK);

values.put(Contacts.ContactMethods.DATA,companyAddr);

getContentResolver().insert(numberUri,values);

    }

    -------------------------------

 private boolean ifEmpty(String input) {

if(input==null||input.length()==0||"".equals(input)){

returntrue;

}else{

if("".equals(input.trim())){

returntrue;

}

returnfalse;

}

 }

-------------------------------------

格式化电话号码的函数

private String formatPhoneNumber(String input) {

if(input.startsWith("1")){

if(input.length()==1){

returninput;

}elseif(input.length()>1&&input.length()<5){

returninput.substring(0,1)+"-"+input.substring(1,input.length());

}elseif(input.length()>=5&&input.length()<8){

returninput.substring(0,1)+"-"+input.substring(1,4)+"-"+input.substring(4,input.length());

}elseif(input.length()>=8){

returninput.substring(0,1)+"-"+input.substring(1,4)+"-"+input.substring(4,7)+"-"+input.substring(7,input.length());

}

}else{

if(input.length()<=3){

returninput;

}elseif(input.length()>3&&input.length()<7){

returninput.substring(0,3)+"-"+input.substring(3,input.length());

}elseif(input.length()>=7){

returninput.substring(0,3)+"-"+input.substring(3,6)+"-"+input.substring(6,input.length());

}

}

return"";

 }

欢迎大家热烈讨论,看android 模拟器 contact部分的源码还有一个途径:

相关推荐