Android 連絡人開發- 儲存連絡人

來源:互聯網
上載者:User

 

最近在開發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)) {
   return true;
  } else {
   if ("".equals(input.trim())) {
    return true;
   }
   return false;
  }
 }

 

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

格式化電話號碼的函數

private String formatPhoneNumber(String input) {
  if (input.startsWith("1")) {
   if (input.length() == 1) {
    return input;
   } else if (input.length() > 1 && input.length() < 5) {
    return input.substring(0, 1) + "-" + input.substring(1, input.length());
   } else if (input.length() >= 5 && input.length() < 8) {
    return input.substring(0, 1) + "-" + input.substring(1, 4) + "-" + input.substring(4, input.length());
   } else if (input.length() >= 8) {
    return input.substring(0, 1) + "-" + input.substring(1, 4) + "-" + input.substring(4, 7) + "-" + input.substring(7, input.length());
   }
  } else {
   if (input.length() <= 3) {
    return input;
   } else if (input.length() > 3 && input.length() < 7) {
    return input.substring(0, 3) + "-" + input.substring(3, input.length());
   } else if (input.length() >= 7) {
    return input.substring(0, 3) + "-" + input.substring(3, 6) + "-" + input.substring(6, input.length());
   }
  }
  return "";
 }

 

 

 

歡迎大家熱烈討論,看android 模擬器 contact部分的源碼還有一個途徑:

http://gitorious.org/0xdroid/packages_apps_contacts

需要使用git工具來下載!

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.