我想从一个已经保存到手机的联系人中检索多个号码。那么如何在android中以编程方式读取一个联系人的号码?
问问题
2254 次
3 回答
2
我已经为此创建了自己的自定义类,它可能会对您有所帮助:
package com.android.addressbook.result;
import java.util.HashMap;
import android.graphics.Bitmap;
public class Contact {
String id = "";
String displayName = "";
String dateOfBirth = "";
String dateOfAnniversary = "";
String nickName = "";
String note = "";
Bitmap image = null;
HashMap<Integer, String> emails;
HashMap<Integer, String> phones;
HashMap<Integer, Address> addresses;
HashMap<Integer, Organization> organizations;
HashMap<Integer, String> im;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(String dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public String getDateOfAnniversary() {
return dateOfAnniversary;
}
public void setDateOfAnniversary(String dateOfAnniversary) {
this.dateOfAnniversary = dateOfAnniversary;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public Bitmap getImage() {
return image;
}
public void setImage(Bitmap image) {
this.image = image;
}
public HashMap<Integer, String> getEmails() {
return emails;
}
public void setEmails(HashMap<Integer, String> emails) {
this.emails = emails;
}
public HashMap<Integer, String> getPhones() {
return phones;
}
public void setPhones(HashMap<Integer, String> phones) {
this.phones = phones;
}
public HashMap<Integer, Address> getAddresses() {
return addresses;
}
public void setAddresses(HashMap<Integer, Address> addresses) {
this.addresses = addresses;
}
public HashMap<Integer, Organization> getOrganizations() {
return organizations;
}
public void setOrganizations(HashMap<Integer, Organization> organizations) {
this.organizations = organizations;
}
public HashMap<Integer, String> getIm() {
return im;
}
public void setIm(HashMap<Integer, String> im) {
this.im = im;
}
/******************************************************************************************/
static class Address {
private String postBox = "";
private String street = "";
private String city = "";
private String state = "";
private String postalCode = "";
private String country = "";
private String neighborhood = "";
public String getPostBox() {
return postBox;
}
public void setPostBox(String postBox) {
this.postBox = postBox;
}
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 getNeighborhood() {
return neighborhood;
}
public void setNeighborhood(String neighborhood) {
this.neighborhood = neighborhood;
}
@Override
public String toString() {
return "Address [postBox=" + postBox + "\n street=" + street
+ "\n city=" + city + "\n state=" + state + "\n postalCode="
+ postalCode + "\n country=" + country + "\n neighborhood="
+ neighborhood + "]";
}
}
/**********************************/
static class Organization {
private String company = "";
private String jobTitle = "";
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getJobTitle() {
return jobTitle;
}
public void setJobTitle(String jobTitle) {
this.jobTitle = jobTitle;
}
@Override
public String toString() {
return "Organization [company=" + company + "\n jobTitle="
+ jobTitle + "]";
}
}
/**********************************/
public static class Email_TYPE {
// Email Type
public static final int HOME = 1;
public static final int WORK = 2;
public static final int OTHER = 3;
public static final int MOBILE = 4;
}
/**********************************/
public static class PHONE_TYPE {
// / Phone Type
public static final int HOME = 1;
public static final int MOBILE = 2;
public static final int WORK = 3;
public static final int FAX_WORK = 4;
public static final int FAX_HOME = 5;
public static final int PAGER = 6;
public static final int OTHER = 7;
}
/**********************************/
public static class ADDRESS_TYPE {
// / Address Type
public static final int HOME = 1;
public static final int WORK = 2;
public static final int OTHER = 3;
}
/**********************************/
public static class ORGANIZATION_TYPE {
// / Organization Type
public static final int WORK = 2;
public static final int OTHER = 3;
}
/**********************************/
public static class IM_TYPE {
public static final int CUSTOM = -1;
public static final int AIM = 0;
public static final int MSN = 1;
public static final int YAHOO = 2;
public static final int SKYPE = 3;
public static final int QQ = 4;
public static final int GOOGLE_TALK = 5;
public static final int ICQ = 6;
public static final int JABBER = 7;
public static final int NETMEETING = 8;
}
@Override
public String toString() {
return "Contact [id=" + id + "\n displayName=" + displayName
+ "\n dateOfBirth=" + dateOfBirth + "\n dateOfAnniversary="
+ dateOfAnniversary + "\n nickName=" + nickName + "\n note="
+ note + "\n image=" + image + "\n emails=" + emails
+ "\n phones=" + phones + "\n addresses=" + addresses
+ "\n organizations=" + organizations + "\n im=" + im + "]";
}
}
PhoneContact.java
package com.android.addressbook.result;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.ContactsContract;
import com.android.addressbook.result.Contact.Address;
import com.android.addressbook.result.Contact.Organization;
public class PhoneContact {
ContentResolver cr;
List<Contact> contactList;
Context context;
public PhoneContact(Context context) {
this.context = context;
cr = context.getContentResolver();
contactList = new ArrayList<Contact>();
readContacts();
}
public void readContacts() {
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Contact contact = new Contact();
// Get contact id (id)
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
contact.setId(id);
// Get contact name (displayName)
String displayName = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
contact.setDisplayName(displayName);
// Get BirthDay (dateOfBirth)
Uri URI_DOB = ContactsContract.Data.CONTENT_URI;
String SELECTION_DOB = ContactsContract.Data.CONTACT_ID
+ " = ? AND "
+ ContactsContract.Data.MIMETYPE
+ " = ? AND "
+ ContactsContract.CommonDataKinds.Event.TYPE
+ "="
+ ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY;
String[] SELECTION_ARRAY_DOB = new String[] {
id,
ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE };
Cursor currDOB = cr.query(URI_DOB, null, SELECTION_DOB,SELECTION_ARRAY_DOB, null);
int indexDob = currDOB.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE);
if (currDOB.moveToNext()) {
String dobStr = currDOB.getString(indexDob);
contact.setDateOfBirth(dobStr);
}
currDOB.close();
// Get Anniversary (dateOfAnniversary)
Uri URI_DOA = ContactsContract.Data.CONTENT_URI;
String SELECTION_DOA = ContactsContract.Data.CONTACT_ID
+ " = ? AND "
+ ContactsContract.Data.MIMETYPE
+ " = ? AND "
+ ContactsContract.CommonDataKinds.Event.TYPE
+ "="
+ ContactsContract.CommonDataKinds.Event.TYPE_ANNIVERSARY;
String[] SELECTION_ARRAY_DOA = new String[] {
id,
ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE };
Cursor currDOA = cr.query(URI_DOA, null, SELECTION_DOA,SELECTION_ARRAY_DOA, null);
int indexDoa = currDOA.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE);
if (currDOA.moveToNext()) {
String doaStr = currDOA.getString(indexDoa);
contact.setDateOfAnniversary(doaStr);
}
currDOA.close();
// Get Nick Nmae(nickName)
Uri URI_NICK_NAME = ContactsContract.Data.CONTENT_URI;
String SELECTION_NICK_NAME = ContactsContract.Data.CONTACT_ID
+ " = ? AND "
+ ContactsContract.Data.MIMETYPE
+ " = ?";
String[] SELECTION_ARRAY_NICK_NAME = new String[] {
id,
ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE };
Cursor currNickName = cr.query(URI_NICK_NAME, null,
SELECTION_NICK_NAME, SELECTION_ARRAY_NICK_NAME,
null);
int indexNickName = currNickName.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.NAME);
if (currNickName.moveToNext()) {
String nickNameStr = currNickName
.getString(indexNickName);
contact.setNickName(nickNameStr);
}
currNickName.close();
// GetNote(note)
Uri URI_NOTE = ContactsContract.Data.CONTENT_URI;
String SELECTION_NOTE = ContactsContract.Data.CONTACT_ID
+ " = ? AND " + ContactsContract.Data.MIMETYPE
+ " = ?";
String[] SELECTION_ARRAY_NOTE = new String[] {
id,
ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE };
Cursor currNote = cr.query(URI_NOTE, null, SELECTION_NOTE,SELECTION_ARRAY_NOTE, null);
int indexNote = currNote.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE);
if (currNote.moveToNext()) {
String noteStr = currNote.getString(indexNote);
contact.setNote(noteStr);
}
currNote.close();
// Get User Image (image)
Uri URI_PHOTO = ContactsContract.Data.CONTENT_URI;
String SELECTION_PHOTO = ContactsContract.Data.CONTACT_ID
+ " = ? AND " + ContactsContract.Data.MIMETYPE
+ " = ?";
String[] SELECTION_ARRAY_PHOTO = new String[] {
id,
ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE };
Cursor currPhoto = cr.query(URI_PHOTO, null,SELECTION_PHOTO, SELECTION_ARRAY_PHOTO, null);
int indexPhoto = currPhoto.getColumnIndex(ContactsContract.CommonDataKinds.Photo.PHOTO);
while (currPhoto.moveToNext()) {
byte[] photoByte = currPhoto.getBlob(indexPhoto);
if (photoByte != null) {
Bitmap bitmap = BitmapFactory.decodeByteArray(photoByte, 0, photoByte.length);
// Getting Caching directory
File cacheDirectory = context.getCacheDir();
// Temporary file to store the contact image
// File tmpFile = new File(cacheDirectory.getPath()
// + "/image_"+id+".png");
File tmpFile = new File(cacheDirectory.getPath()+ "/image_.png");
// The FileOutputStream to the temporary file
try {
FileOutputStream fOutStream = new FileOutputStream(tmpFile);
// Writing the bitmap to the temporary file as png file
bitmap.compress(Bitmap.CompressFormat.PNG, 100,fOutStream);
// Flush the FileOutputStream
fOutStream.flush();
// Close the FileOutputStream
fOutStream.close();
} catch (Exception e) {
e.printStackTrace();
}
// String photoPath = tmpFile.getPath();
contact.setImage(bitmap);
}
}
currPhoto.close();
// Get Email and Type.... (<HashMap<Integer, String> emails)
Uri URI_EMAIL = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
String SELECTION_EMAIL = ContactsContract.CommonDataKinds.Email.CONTACT_ID+ " = ?";
String[] SELECTION_ARRAY_EMAIL = new String[] { id };
Cursor emailCur = cr.query(URI_EMAIL, null,SELECTION_EMAIL, SELECTION_ARRAY_EMAIL, null);
int indexEmail = emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
int indexEmailType = emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE);
if (emailCur.getCount() > 0) {
HashMap<Integer, String> emailMap = new HashMap<Integer, String>();
while (emailCur.moveToNext()) {
// This would allow you get several email addresses,
// if the email addresses were stored in an array
String emailStr = emailCur.getString(indexEmail);
String emailTypeStr = emailCur.getString(indexEmailType);
emailMap.put(Integer.parseInt(emailTypeStr),emailStr);
}
contact.setEmails(emailMap);
}
emailCur.close();
// Get Phone Number....(HashMap<Integer, String>phones)
Uri URI_PHONE = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String SELECTION_PHONE = ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = ?";
String[] SELECTION_ARRAY_PHONE = new String[] { id };
Cursor currPhone = cr.query(URI_PHONE, null,SELECTION_PHONE, SELECTION_ARRAY_PHONE, null);
int indexPhoneNo = currPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int indexPhoneType = currPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
if (currPhone.getCount() > 0) {
HashMap<Integer, String> phoneMap = new HashMap<Integer, String>();
while (currPhone.moveToNext()) {
String phoneNoStr = currPhone.getString(indexPhoneNo);
String phoneTypeStr = currPhone.getString(indexPhoneType);
phoneMap.put(Integer.parseInt(phoneTypeStr),phoneNoStr);
}
contact.setPhones(phoneMap);
}
currPhone.close();
// Get Postal Address....(HashMap<Integer, Address> addresses)
Uri URI_ADDRESS = ContactsContract.Data.CONTENT_URI;
String SELECTION_ADDRESS = ContactsContract.Data.CONTACT_ID
+ " = ? AND " + ContactsContract.Data.MIMETYPE
+ " = ?";
String[] SELECTION_ARRAY_ADDRESS = new String[] {
id,
ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE };
Cursor currAddr = cr.query(URI_ADDRESS, null,SELECTION_ADDRESS, SELECTION_ARRAY_ADDRESS, null);
int indexAddType = currAddr
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE);
int indexStreet = currAddr
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET);
int indexPOBox = currAddr
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX);
int indexNeighbor = currAddr
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.NEIGHBORHOOD);
int indexCity = currAddr
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY);
int indexRegion = currAddr
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION);
int indexPostCode = currAddr
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE);
int indexCountry = currAddr
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY);
if (currAddr.getCount() > 0) {
HashMap<Integer, Address> addressMap = new HashMap<Integer, Contact.Address>();
while (currAddr.moveToNext()) {
Contact.Address address = new Contact.Address();
String typeStr = currAddr.getString(indexAddType);
address.setStreet(currAddr.getString(indexStreet));
address.setNeighborhood(currAddr.getString(indexNeighbor));
address.setPostalCode(currAddr.getString(indexPostCode));
address.setPostBox(currAddr.getString(indexPOBox));
address.setCity(currAddr.getString(indexCity));
address.setState(currAddr.getString(indexRegion));
address.setCountry(currAddr.getString(indexCountry));
addressMap.put(Integer.parseInt(typeStr), address);
}
contact.setAddresses(addressMap);
}
currAddr.close();
// Get Organization (HashMap<Integer, Organization> organizations)
Uri URI_ORGNIZATION = ContactsContract.Data.CONTENT_URI;
String SELECTION_ORGNIZATION = ContactsContract.Data.CONTACT_ID
+ " = ? AND "
+ ContactsContract.Data.MIMETYPE
+ " = ?";
String[] SELECTION_ARRAY_ORGNIZATION = new String[] {
id,
ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE };
Cursor currOrg = cr.query(URI_ORGNIZATION, null,
SELECTION_ORGNIZATION, SELECTION_ARRAY_ORGNIZATION,
null);
int indexOrgType = currOrg
.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TYPE);
int indexOrgName = currOrg
.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DATA);
int indexOrgTitle = currOrg
.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE);
if (currOrg.getCount() > 0) {
HashMap<Integer, Organization> orgMap = new HashMap<Integer, Contact.Organization>();
while (currOrg.moveToNext()) {
Contact.Organization organization = new Organization();
String orgTypeStr = currOrg.getString(indexOrgType);
organization.setCompany(currOrg.getString(indexOrgName));
organization.setJobTitle(currOrg.getString(indexOrgTitle));
orgMap.put(Integer.parseInt(orgTypeStr),organization);
}
contact.setOrganizations(orgMap);
}
currOrg.close();
// Get Instant Messenger..... (HashMap<Integer, String> im)
Uri URI_IM = ContactsContract.Data.CONTENT_URI;
String SELECTION_IM = ContactsContract.Data.CONTACT_ID
+ " = ? AND " + ContactsContract.Data.MIMETYPE
+ " = ?";
String[] SELECTION_ARRAY_IM = new String[] {
id,
ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE };
Cursor currIM = cr.query(URI_IM, null, SELECTION_IM,SELECTION_ARRAY_IM, null);
int indexName = currIM
.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA);
int indexType = currIM
.getColumnIndex(ContactsContract.CommonDataKinds.Im.PROTOCOL);
if (currIM.getCount() > 0) {
HashMap<Integer, String> imMap = new HashMap<Integer, String>();
while (currIM.moveToNext()) {
String imNameStr = currIM.getString(indexName);
String imTypeStr = currIM.getString(indexType);
imMap.put(Integer.parseInt(imTypeStr), imNameStr);
}
contact.setIm(imMap);
}
currIM.close();
/*****************************************/
contactList.add(contact);
}
}
}
cur.close();
}
public List<Contact> getAllContacts() {
return contactList;
}
}
最后在您的活动中使用它:
PhoneContact pCon = new PhoneContact(context);
List<Contact> conList = pCon.getAllContacts();
HashMap<Integer, String> phones = conList.get(0).getPhones();
String home = phones.get(Contact.PHONE_TYPE.HOME);
于 2013-02-04T08:42:08.943 回答
0
这是教程。我想它可能会帮助你。检查一次
于 2013-02-04T05:52:23.393 回答
0
希望这个片段可以帮助你
String id , name;
ContentResolver cr = getContentResolver();
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, sortOrder);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
name = cur.getString( cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.i(tag, "Id is "+ id+"\t Name is"+name);
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0){
Cursor pCur = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
// this second loop will retrieve all the contact numbers for a paricular contact id
while (pCur.moveToNext()) {
// Do something with phones
int phNumber = pCur.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER);
String phn = pCur.getString(phNumber);
Log.i("phn number", phn);
}
pCur.close();
}
}
}
于 2013-02-04T05:53:47.740 回答