Working With Android Contacts

1.x Data access class

This class is made up of the code explained covering version 1.x of the Android contact API. As with the 2.0 class this class extends the ContactAPI wrapper class and will be invoked by it if the 1.x version of Android is in use.
package com.higherpass.android.ContactAPI;

import java.util.ArrayList;

import com.higherpass.android.ContactAPI.objects.Address;
import com.higherpass.android.ContactAPI.objects.Contact;
import com.higherpass.android.ContactAPI.objects.ContactList;
import com.higherpass.android.ContactAPI.objects.Email;
import com.higherpass.android.ContactAPI.objects.IM;
import com.higherpass.android.ContactAPI.objects.Organization;
import com.higherpass.android.ContactAPI.objects.Phone;

import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.provider.Contacts;
import android.provider.Contacts.People;

public class ContactAPISdk3 extends ContactAPI {

private Cursor cur;
private ContentResolver cr;

public Cursor getCur() {
return cur;
}

public void setCur(Cursor cur) {
this.cur = cur;
}

public ContentResolver getCr() {
return cr;
}

public void setCr(ContentResolver cr) {
this.cr = cr;
}

public Intent getContactIntent() {
return(new Intent(Intent.ACTION_PICK, People.CONTENT_URI));
}

public ContactList newContactList() {
ContactList contacts = new ContactList();
String id;

this.cur = this.cr.query(People.CONTENT_URI,
null, null, null, null);
if (this.cur.getCount() > 0) {
while (cur.moveToNext()) {
Contact c = new Contact();
id = cur.getString(cur.getColumnIndex(People._ID));
c.setId(id);
c.setDisplayName(cur.getString(cur.getColumnIndex(People.DISPLAY_NAME)));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(People.PRIMARY_PHONE_ID))) > 0) {
c.setPhone(this.getPhoneNumbers(id));
}
c.setEmail(this.getEmailAddresses(id));
ArrayList<String> notes = new ArrayList<String>();
notes.add(cur.getString(cur.getColumnIndex(People.NOTES)));
c.setNotes(notes);
c.setAddresses(this.getContactAddresses(id));
c.setImAddresses(this.getIM(id));
c.setOrganization(this.getContactOrg(id));
contacts.addContact(c);
}
}
return(contacts);
}

public ArrayList<Phone> getPhoneNumbers(String id) {
ArrayList<Phone> phones = new ArrayList<Phone>();

Cursor pCur = this.cr.query(
Contacts.Phones.CONTENT_URI,
null,
Contacts.Phones.PERSON_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
phones.add(new Phone(
pCur.getString(pCur.getColumnIndex(Contacts.Phones.NUMBER))
, pCur.getString(pCur.getColumnIndex(Contacts.Phones.TYPE))
));

}
pCur.close();
return(phones);
}

public ArrayList<Email> getEmailAddresses(String id) {
ArrayList<Email> emails = new ArrayList<Email>();

Cursor emailCur = this.cr.query(
Contacts.ContactMethods.CONTENT_EMAIL_URI,
null,
Contacts.ContactMethods.PERSON_ID + " = ?",
new String[]{id}, null);
while (emailCur.moveToNext()) {
// This would allow you get several email addresses
Email e = new Email(emailCur.getString(emailCur.getColumnIndex(Contacts.ContactMethods.DATA))
,emailCur.getString(emailCur.getColumnIndex(Contacts.ContactMethods.CONTENT_EMAIL_TYPE))
);
emails.add(e);
}
emailCur.close();
return(emails);
}

public ArrayList<Address> getContactAddresses(String id) {
ArrayList<Address> addrList = new ArrayList<Address>();

String where = Contacts.ContactMethods.PERSON_ID + " = ? AND " + Contacts.ContactMethods.KIND + " = ?";
String[] whereParameters = new String[]{id,
Contacts.ContactMethods.CONTENT_POSTAL_ITEM_TYPE};

Cursor addrCur = this.cr.query(Contacts.ContactMethods.CONTENT_URI, null, where, whereParameters, null);
while(addrCur.moveToNext()) {
String addr = addrCur.getString(addrCur.getColumnIndex(Contacts.ContactMethodsColumns.DATA));
String type = addrCur.getString(addrCur.getColumnIndex(Contacts.ContactMethodsColumns.TYPE));
Address a = new Address(addr, type);
addrList.add(a);
}
addrCur.close();
return(addrList);
}

public ArrayList<IM> getIM(String id) {
ArrayList<IM> imList = new ArrayList<IM>();
String where = Contacts.ContactMethods.PERSON_ID + " = ? AND " + Contacts.ContactMethods.KIND + " = ?";
String[] whereParameters = new String[]{id,
Contacts.ContactMethods.CONTENT_IM_ITEM_TYPE};

Cursor imCur = this.cr.query(Contacts.ContactMethods.CONTENT_URI, null, where, whereParameters, null);
if (imCur.moveToFirst()) {
String imName = imCur.getString(imCur.getColumnIndex(Contacts.ContactMethodsColumns.DATA));
String imType = imCur.getString(imCur.getColumnIndex(Contacts.ContactMethodsColumns.TYPE));
if (imName.length() > 0) {
IM im = new IM(imName, imType);
imList.add(im);
}
}
imCur.close();
return(imList);
}

public Organization getContactOrg(String id) {
Organization org = new Organization();
String where = Contacts.ContactMethods.PERSON_ID + " = ?";
String[] whereParameters = new String[]{id};

Cursor orgCur = this.cr.query(Contacts.Organizations.CONTENT_URI, null, where, whereParameters, null);

if (orgCur.moveToFirst()) {
String orgName = orgCur.getString(orgCur.getColumnIndex(Contacts.Organizations.COMPANY));
String title = orgCur.getString(orgCur.getColumnIndex(Contacts.Organizations.TITLE));
if (orgName.length() > 0) {
org.setOrganization(orgName);
org.setTitle(title);
}
}
orgCur.close();
return(org);
}
}

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Android(安卓)开发-获取SD卡所有指定类型
  2. Android(安卓)6.0 MT流程
  3. 谷歌Android手机惊现高危软件缺陷
  4. 浅析Binder--源码系列
  5. Android的Activity什么时候会调用onCreat
  6. Android(安卓)屏幕(View)坐标系统
  7. Android的基本组件
  8. android二维码扫描(最近做的项目中用到的
  9. android编译系统makefile
  10. 介绍一种不同流式标签的实现方式