`
JasonShieh
  • 浏览: 527532 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Android联系人数据库全解析(3)

阅读更多
Working With Android Contacts
轻松玩转Android联系人
Gluing it together
把两个版本结合起来
To put this together into an application there are a few glue pieces that need to
be setup along with creating classes to manage accessing the data. First we need to create a set of classes to hold the data. Also we'll create a class to handle 2.0 API calls and a class to handle 1.6 and earlier API calls. There's also a wrapper class that determines and loads the proper class.
把两个版本的联系人操作整合起来,我们需要建立一些读数据的类。首先,我们需要建立一些存储数据的类,我们还需要一个类来分别对2.0的API和1.6以前的API进行处理的类。我们还必须建立一个解析类然后去加载合适的类。
Contact Data Classes
联系人数据类
The contact classes are a series of classes to hold a list of contacts. The list is stored in the class ContactList that maintains an ArrayList of Contacts. The Contact objects are represented in the Contact class. The contact class stores all the data from the Android contact record. In addition to the ContactList and Contact classes there are specialized classes to represent some of the record data.
联系人类是一系列抽象了联系人列表的类。这个ContactList中列表保存了联系人的列表。联系人对象被封装在联系人类中,联系人类存储了Android系统联系人记录中的所有数据。除了ContactList和Contact类之外,还有一些特殊的类来封装其他的记录数据。
We will create classes to represent the address, email, instant messenger, phone number, and organization(s). Most of these classes are mere data storage classes with variables and getter/setters.
我们将建立类来封装“地址”、“电子邮件”、“实时消息”、“电话号码”、和“分组”。这些类中大多数都仅仅有一些属性和一些getter/setter方法。
ContactList

The ContactList class is a very basic class designed to hold an ArrayList of instances of the Contact class below. We've left this class very plain and ready to be expanded to suit your needs.
package com.higherpass.android.ContactAPI.objects;

import java.util.ArrayList;

public class ContactList {

private ArrayList<Contact> contacts = new ArrayList<Contact>();

public ArrayList<Contact> getContacts() {
return contacts;
}

public void setContacts(ArrayList<Contact> contacts) {
this.contacts = contacts;
}

public void addContact(Contact contact) {
this.contacts.add(contact);
}
 
public ContactList() {

}

}
Contact

The Contact class is used to store the details about each contact. There are a series of private class variables to hold this data. Singular data such as name and database ID are stored as strings. Complex data is stored either as an instance or ArrayList of data specific classes. This class is mainly getters and setters with a few methods to add to the internal ArrayLists.
package com.higherpass.android.ContactAPI.objects;

import java.util.ArrayList;

public class Contact {
private String id;
private String displayName;
private ArrayList<Phone> phone;
private ArrayList<Email> email;
private ArrayList<String> notes;
private ArrayList<Address> addresses = new ArrayList<Address>();
private ArrayList<IM> imAddresses;
private Organization organization;
 

public Organization getOrganization() {
return organization;
}
public void setOrganization(Organization organization) {
this.organization = organization;
}
public ArrayList<IM> getImAddresses() {
return imAddresses;
}
public void setImAddresses(ArrayList<IM> imAddresses) {
this.imAddresses = imAddresses;
  }
public void addImAddresses(IM imAddr) {
this.imAddresses.add(imAddr);
}
public ArrayList<String> getNotes() {
return notes;
}
public void setNotes(ArrayList<String> notes) {
this.notes = notes;
}
public void addNote(String note) {
this.notes.add(note);
}
public ArrayList<Address> getAddresses() {
return addresses;
}
public void setAddresses(ArrayList<Address> addresses) {
this.addresses = addresses;
}
public void addAddress(Address address) {
this.addresses.add(address);
}
public ArrayList<Email> getEmail() {
return email;
}
public void setEmail(ArrayList<Email> email) {
this.email = email;
}
public void addEmail(Email e) {
this.email.add(e);
}
public String getId() {
return id;
}
public void setId(String id) {
  this.id = id;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String dName) {
this.displayName = dName;
}
public ArrayList<Phone> getPhone() {
return phone;
}
public void setPhone(ArrayList<Phone> phone) {
this.phone = phone;
}
public void addPhone(Phone phone) {
this.phone.add(phone);
}
}
Address

The Address class is the only class in the ContactList framework that actually does any work. Due to differences in data storage between 1.x and 2.0 versions of Android the Address class has to determine if the input address is free-form from 1.x or was input from the formatted data structure from 2.0. Android 2.0 has individual data columns for PO-Box, street, city, region, postal code, country while 1.x was just a text field with the entire address. This is handled in the toString() method that returns a free-form address from either input type. Unfortunately when using Android 1.x all the individual address getters will return null.
package com.higherpass.android.ContactAPI.objects;

public class Address {
private String poBox;
private String street;
private String city;
private String state;
private String postalCode;
private String country;
private String type;
private String asString = "";

public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getPoBox() {
return poBox;
}
public void setPoBox(String poBox) {
this.poBox = poBox;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getPostalCode() {
return postalCode;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String toString() {
if (this.asString.length() > 0) {
return(this.asString);
} else {
String addr = "";
if (this.getPoBox() != null) {
addr = addr + this.getPoBox() + "n";
}
if (this.getStreet() != null) {
addr = addr + this.getStreet() + "n";
}
if (this.getCity() != null) {
addr = addr + this.getCity() + ", ";
}
if (this.getState() != null) {
addr = addr + this.getState() + " ";
}
if (this.getPostalCode() != null) {
addr = addr + this.getPostalCode() + " ";
}
if (this.getCountry() != null) {
addr = addr + this.getCountry();
}
return(addr);
}
}

public Address(String asString, String type) {
this.asString = asString;
this.type = type;
}

public Address(String poBox, String street, String city, String state,
String postal, String country, String type) {
this.setPoBox(poBox);
  this.setStreet(street);
this.setCity(city);
this.setState(state);
this.setPostalCode(postal);
this.setCountry(country);
this.setType(type);
}
}

Email

Another getter/setter and data storage class. The email class stores the email address and address type (work, home, etc).
package com.higherpass.android.ContactAPI.objects;

public class Email {
private String address;
private String type;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getType() {
return type;
}
public void setType(String t) {
this.type = t;
}

public Email(String a, String t) {
this.address = a;
this.type = t;
}
}
IM

Class to hold instant messenger data.
package com.higherpass.android.ContactAPI.objects;

public class IM {
private String name;
private String type;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}

public IM(String name, String type) {
this.name = name;
this.type = type;
}
}
Organization

Class to hold the contacts organizational data.
package com.higherpass.android.ContactAPI.objects;

public class Organization {
private String organization = "";
private String title = "";
public String getOrganization() {
return organization;
}
public void setOrganization(String organization) {
this.organization = organization;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}

public Organization() {

}
public Organization(String org, String title) {
this.organization = org;
this.title = title;
}
}
Phone

Class to hold the phone records.
package com.higherpass.android.ContactAPI.objects;

public class Phone {
private String number;
private String type;

public String getNumber() {
return number;
}

public void setNumber(String number) {
this.number = number;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public Phone(String n, String t) {
this.number = n;
this.type = t;
}

}
分享到:
评论

相关推荐

    android 解析 开机加载SIM联系人及对SIM卡联系人的操作

    ### Android解析:开机加载SIM联系人及对SIM卡联系人的操作 #### 一、概述 在Android系统中,用户能够方便地访问并管理SIM卡上的联系人信息,这些功能背后涉及到了一系列复杂的交互机制和技术实现。本文将详细介绍...

    Android@联系人功能

    这通常涉及到读取Android的ContactsContract数据库,筛选出用户可能需要@的联系人信息,如姓名和唯一标识。 3. **显示联系人选择界面**:创建一个Dialog或者BottomSheet,展示联系人列表。列表项应包含联系人头像、...

    android联系人二维码生成以及扫描

    综上所述,"android联系人二维码生成以及扫描"这个主题涵盖了Android应用开发中的多个重要技术点,包括二维码库的使用、数据编码与解码、相机权限处理以及用户界面设计等。开发者通过学习和实践这些内容,能够增强其...

    Android联系人总结

    Android系统的联系人管理功能是移动通信应用中的重要组成部分,本文将详细解析Android系统中与联系人相关的数据库架构与数据存储方式,帮助开发者更好地理解和掌握Android联系人数据库的工作原理及其实现细节。...

    Android通过蓝牙使用Pbap协议读取通讯录

    VCard数据在接收到OBEX GET请求响应后会被解析,然后可以存储到Android系统的联系人数据库中。 在实际开发过程中,你需要遵循以下步骤: 1. 检查并启用蓝牙,搜索支持PBAP的远程设备,并进行配对。 2. 创建...

    Android 异步联系人备份与恢复Demo

    - 将VCard导入到联系人:Android提供Intent ACTION_INSERT_OR_REPLACE,通过startActivityForResult()启动这个Intent,传递解析后的VCard数据,系统会自动处理导入过程。也可以使用ContentResolver的insert()方法,...

    android 支持分组和联系人展示的一个小例子(类似QQ首页)

    在Android中,联系人数据通常存储在系统提供的Contacts数据库中,可以通过ContentResolver进行查询和操作。分组则是联系人的组织形式,可以将相关的联系人归类到同一个分组下,便于管理和查找。 在"ContactProject...

    android扫描名片并解析名片源码

    同时,允许用户导出名片数据为CSV或VCF格式,便于导入其他联系人管理软件。 以上就是"android扫描名片并解析名片源码"中涉及的主要知识点,涵盖了从图像处理到数据存储的全过程,体现了Android应用开发的多个技术...

    Android应用源码高仿QQ客户端加服务端加数据库全套

    2. **用户界面(UI)设计**:仿QQ客户端意味着需要创建与QQ相似的用户界面,包括聊天界面、联系人列表、消息通知等。这涉及到对Android的自定义View和Adapter的深入理解和使用,以及对Material Design设计规范的理解。...

    Android短信联系人备份并上传

    在Android开发中,有时我们需要对用户的短信、联系人和通话记录等重要数据进行备份,以防止数据丢失。本文将深入探讨如何实现这个功能,并讲解如何将备份的数据上传到JavaWeb服务器。 首先,我们要关注的是“短信和...

    android联系人contacts模块的学习总结

    以下是对Android联系人contacts模块及相关技术的学习总结。 首先,Android平台提供了多种数据存储方式,这包括文件方式、SharedPreferences以及嵌入式关系型数据库SQLite。对于文件存储方式,Android提供了标准的...

    android 批量插入联系人、短信、通话记录,安装版,apk格式

    在Android系统中,批量操作是常见的需求,尤其是在处理大量数据如联系人、短信和通话记录时。本项目提供了一个解决方案,允许用户批量插入这些信息,特别解决了自Android 4.4版本后插入短信的限制问题。这个解决方案...

    安卓通讯录联系人打电话归属地相关-集成了省份城市银行银行支行联行号的android数据库使用方便字段可以通过三方软件进行查看.rar

    这里的数据库显然包含了联系人的详细信息,包括但不限于姓名、电话号码、归属地以及与银行相关的附加信息。 2. **省份城市信息**:在数据库中集成省份和城市信息,意味着可以方便地获取联系人的地理位置,这对于...

    Android通讯录管理(获取联系人、通话记录、短信消息)

    这篇教程将深入探讨如何在Android应用中获取联系人信息、通话记录以及短信消息。首先,我们来了解一下基本概念。 1. **获取联系人信息**: Android提供了一套完整的API来访问和操作联系人数据。主要涉及`...

    安卓通讯录联系人打电话归属地相关-android手机号码归属地查询源码内附SQlite数据库并且有查询示例。.zip

    在Android平台上,开发一款应用程序来查询手机通讯录中的联系人号码归属地是一项常见的需求。这个压缩包提供的资源,包括源码、SQLite数据库以及相关的帮助文档,正是为了满足这样的需求。以下将详细介绍这些资源的...

    Eclipse下编译Android自带联系人应用

    3. 在Eclipse中,选择“File” -&gt; “Import” -&gt; “Existing Android Code into Workspace”,然后导航到你克隆的AOSP源码目录下的`packages/apps/Contacts`,这是联系人应用的源码位置。 **编译过程** 1. 在...

    Android程序研发源码Android 联系人快速索引源码.zip

    这个"Android程序研发源码Android 联系人快速索引源码.zip"文件包含的是一个关于如何实现Android联系人应用快速索引功能的源代码示例。快速索引功能使得用户可以迅速定位到特定联系人,极大地提高了用户体验。下面...

    android-vcard-1.3.rar_VCardParser_V30_VCardSourceDetector_androi

    3. **数据转换**:将Android的联系人对象模型转换为Web服务器可接受的格式,如JSON或XML。 4. **网络通信**:通过HTTP请求将转换后的数据发送到Web服务器,同时处理可能出现的网络异常。 5. **服务器端处理**:...

    android4.0联系人Contacts源代码

    《Android 4.0 联系人Contacts源代码解析》 在Android系统中,联系人管理是核心功能之一,其背后的实现机制涉及到许多关键知识点。本篇文章将深入探讨Android 4.0(Ice Cream Sandwich)中Contacts应用的源代码,...

    Android 2.1 从SD卡导入联系人 vcf

    这个过程包括读取.vcf文件、解析文件内容、创建或更新联系人条目,并最终将它们插入到联系人数据库中。 1. **读取SD卡文件**: Android提供`Environment.getExternalStorageDirectory()`方法获取SD卡的根目录,...

Global site tag (gtag.js) - Google Analytics