`
zsnlovewl
  • 浏览: 173176 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

阅读更多

最近在开发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部分的源码还有一个途径:

分享到:
评论

相关推荐

    Android开发实验---通讯录.docx

    解决添加联系人的表中添加数据以及读取已保存的数据 3. 实现通讯录的基本功能的具体实现。 4. 对通讯录界面的具体设计,给人一种方便的操作通道。 5. 灵活应用各种系统库函数实现完整的通讯录。 Android开发实验---...

    Android高级编程--源代码

    9.1.4 在线状态和联系人列表简介 282 9.1.5 管理聊天会话 285 9.1.6 发送和接收数据信息 289 9.2 SMS简介 291 9.2.1 在应用程序中使用SMS 291 9.2.2 发送SMS信息 291 9.2.3 监听SMS消息 294 9.2.4 紧急响应...

    新版Android开发教程.rar

    Android Android Android Android 开发背景 � 计算技术、无线接入技术的发展,使嵌入式系统逐渐有能力对桌面系统常规业务进行支持。 � 谷歌长期以来奉行的移动发展战略:通过与全球各地的手机制造商和移动运营商...

    安卓联系人app

    android studio 开发的一款手机联系人app, 该软件功能主要包括以下八个方面: 1. 获取访问手机联系人权限,读取手机原有联系人到App中. 2.新建联系人号码和姓名,并刷新保存。 3.编辑选择性删除已创建的联系人...

    传智播客_Andorid_20天精通Android开发视频_第13天视频_视频_10_保存联系人数据完成

    传智播客_Andorid_20天精通Android开发视频_第13天视频_视频_10_保存联系人数据完成.avi

    传智播客_Andorid_20天精通Android开发视频_第13天视频_视频_12_保存联系人数据_界面实现

    传智播客_Andorid_20天精通Android开发视频_第13天视频_视频_12_保存联系人数据_界面实现.avi

    黑马程序员 安卓学院 万元哥项目经理 分享220个代码实例

    |--内容提供者之联系人读写与批量操作 |--内容提供者之获取通话记录 |--内容提供者的定义 |--写入联系人信息 |--利用FinalHttp实现多线程断点续传 |--加密之MD5 |--动画Animation详解 |--动画之view左右抖动 |--动画...

    android开发实例大全_王东华

    实例060: 保存联系人信息 194 第5章 电话和短信实例集锦 197 实例061: 编写一个拨号程序 197 实例062: 发送一条短信 200 实例063: 单击按钮实现拨号功能 203 实例064: 一个通用发短信程序 206 实例065: 收到...

    传智播客的android开发源代码

    22_访问通信录中的联系人和添加联系人.avi 所在项目:contacts 23_网络通信之网络图片查看器.avi 所在项目:netimage & Web端应用:web 24_网络通信之网页源码查看器.avi 所在项目:HtmlViewer & Web端应用:web 25_...

    《Android高级编程》

    第9章 P2P通信 9.1 Android即时消息简介 9.1.1 使用GTalk服务 9.1.2 和GTalk服务绑定 9.1.3 建立GTalk连接,开始一个IM会话 9.1.4 在线状态和联系人列表简介 9.1.5 管理聊天会话 9.1.6 发送和接收数据信息 9.2 SMS...

    8天快速掌握Android系列视频之04_开发与 运行(卸载)第一个ANDROID应用

    信录中的联系人和添加联系人,23_网络通信之网络图片查看器,24_网络通信之网页源码查看器,25_网络通信之资讯客户 端,26_采用JSON格式返回数据给资讯客户端,27_网络通信之通过GET和POST方式提交参数给web应用,...

    精通ANDROID 3(中文版)1/2

    1.5 使用Android SDK开发最终用户应用程序  1.5.1 Android模拟器  1.5.2 Android UI  1.5.3 Android基础组件  1.5.4 高级UI概念  1.5.5 Android Service组件  1.5.6 Android媒体和电话组件  1.5.7 ...

    工程硕士学位论文 基于Android+HTML5的移动Web项目高效开发探究

    鉴于市场上用户的手机型号、种类、屏幕分辨率等参差不齐,传统方式根据主流系统分别开发相应的系统耗时又耗力,为了高效开发并节约开发项目成本,本文采用Android+HTML5相结合的方式进行移动端Web系统的设计研发工作...

    基于语音识别的老年人防诈骗系统(信安大赛代码保存)(android+flask).zip

    Android 项目是使用 Android 操作系统和相关开发工具开发的一款移动应用程序。Android 平台提供了丰富的功能和接口,开发人员可以使用 Java 或 Kotlin 等编程语言编写 Android 应用程序。Android 项目也可以是针对...

    Android高级编程.pdf

    9.1.4 在线状态和联系人列表简介 9.1.5 管理聊天会话 9.1.6 发送和接收数据信息 9.2 SMS简介 9.2.1 在应用程序中使用SMS 9.2.2 发送SMS信息 9.2.3 监听SMS消息 9.2.4 紧急响应的SMS示例 9.2.5 紧急响应自动化 9.3 ...

    8天快速掌握Android教程源码

    22_访问通信录中的联系人和添加联系人.avi 所在项目:contacts 23_网络通信之网络图片查看器.avi 所在项目:netimage & Web端应用:web 24_网络通信之网页源码查看器.avi 所在项目:HtmlViewer & Web端应用:web 25_...

    android-simply-tone-generator:发出声音,任何声音。 包括DTMF支持

    变更日志1.7(主动开发) 与联系人集成1.6(2015-02-09) 新功能:能够录制无限音! 新功能:能够为已保存的音调创建标题! 新功能:能够编辑保存的记录增强功能:现在更容易看到Backspace按钮代码结构净简化...

Global site tag (gtag.js) - Google Analytics