日期:2014-05-16  浏览次数:20463 次

Android联系人数据库全解析(5)
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))