android开发怎样获取通讯录联系人信息
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android开发怎样获取通讯录联系人信息相关的知识,希望对你有一定的参考价值。
参考技术A 在android开发中,我经常会遇到需要获取手机通讯录联系人信息,Android手机的通讯录联系人全部都存在系统的数据库中,如果须要获得通讯里联系人的信息就须要访问系统的数据库,才能将信息获取出来。下面直接贴出代码供大家参考:
程序文件java代码:
import java.io.InputStream;
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.Photo;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class ContactsActivity extends ListActivity
Context mContext = null;
/**获取库Phon表字段**/
private static final String[] PHONES_PROJECTION = new String[]
Phone.DISPLAY_NAME, Phone.NUMBER, Photo.PHOTO_ID,Phone.CONTACT_ID ;
/**联系人显示名称**/
private static final int PHONES_DISPLAY_NAME_INDEX = 0;
/**电话号码**/
private static final int PHONES_NUMBER_INDEX = 1;
/**头像ID**/
private static final int PHONES_PHOTO_ID_INDEX = 2;
/**联系人的ID**/
private static final int PHONES_CONTACT_ID_INDEX = 3;
/**联系人名称**/
private ArrayList<String> mContactsName = new ArrayList<String>();
/**联系人头像**/
private ArrayList<String> mContactsNumber = new ArrayList<String>();
/**联系人头像**/
private ArrayList<Bitmap> mContactsPhonto = new ArrayList<Bitmap>();
ListView mListView = null;
MyListAdapter myAdapter = null;
@Override
public void onCreate(Bundle savedInstanceState)
mContext = this;
mListView = this.getListView();
/**得到手机通讯录联系人信息**/
getPhoneContacts();
myAdapter = new MyListAdapter(this);
setListAdapter(myAdapter);
mListView.setOnItemClickListener(new OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long id)
//调用系统方法拨打电话
Intent dialIntent = new Intent(Intent.ACTION_CALL, Uri
.parse("tel:" + mContactsNumber.get(position)));
startActivity(dialIntent);
);
super.onCreate(savedInstanceState);
/**得到手机通讯录联系人信息**/
private void getPhoneContacts()
ContentResolver resolver = mContext.getContentResolver();
// 获取手机联系人
Cursor phoneCursor = resolver.query(Phone.CONTENT_URI,PHONES_PROJECTION, null, null, null);
if (phoneCursor != null)
while (phoneCursor.moveToNext())
//得到手机号码
String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX);
//当手机号码为空的或者为空字段 跳过当前循环
if (TextUtils.isEmpty(phoneNumber))
continue;
//得到联系人名称
String contactName = phoneCursor.getString(PHONES_DISPLAY_NAME_INDEX);
//得到联系人ID
Long contactid = phoneCursor.getLong(PHONES_CONTACT_ID_INDEX);
//得到联系人头像ID
Long photoid = phoneCursor.getLong(PHONES_PHOTO_ID_INDEX);
//得到联系人头像Bitamp
Bitmap contactPhoto = null;
//photoid 大于0 表示联系人有头像 如果没有给此人设置头像则给他一个默认的
if(photoid > 0 )
Uri uri =ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,contactid);
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(resolver, uri);
contactPhoto = BitmapFactory.decodeStream(input);
else
contactPhoto = BitmapFactory.decodeResource(getResources(), R.drawable.contact_photo);
mContactsName.add(contactName);
mContactsNumber.add(phoneNumber);
mContactsPhonto.add(contactPhoto);
phoneCursor.close();
/**得到手机SIM卡联系人人信息**/
private void getSIMContacts()
ContentResolver resolver = mContext.getContentResolver();
// 获取Sims卡联系人
Uri uri = Uri.parse("content://icc/adn");
Cursor phoneCursor = resolver.query(uri, PHONES_PROJECTION, null, null,
null);
if (phoneCursor != null)
while (phoneCursor.moveToNext())
// 得到手机号码
String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX);
// 当手机号码为空的或者为空字段 跳过当前循环
if (TextUtils.isEmpty(phoneNumber))
continue;
// 得到联系人名称
String contactName = phoneCursor
.getString(PHONES_DISPLAY_NAME_INDEX);
//Sim卡中没有联系人头像
mContactsName.add(contactName);
mContactsNumber.add(phoneNumber);
phoneCursor.close();
class MyListAdapter extends BaseAdapter
public MyListAdapter(Context context)
mContext = context;
public int getCount()
//设置绘制数量
return mContactsName.size();
@Override
public boolean areAllItemsEnabled()
return false;
public Object getItem(int position)
return position;
public long getItemId(int position)
return position;
public View getView(int position, View convertView, ViewGroup parent)
ImageView iamge = null;
TextView title = null;
TextView text = null;
if (convertView == null)
convertView = LayoutInflater.from(mContext).inflate(
R.layout.colorlist, null);
iamge = (ImageView) convertView.findViewById(R.id.color_image);
title = (TextView) convertView.findViewById(R.id.color_title);
text = (TextView) convertView.findViewById(R.id.color_text);
//绘制联系人名称
title.setText(mContactsName.get(position));
//绘制联系人号码
text.setText(mContactsNumber.get(position));
//绘制联系人头像
iamge.setImageBitmap(mContactsPhonto.get(position));
return convertView;
iOS-获取通讯录联系人信息
头文件
#import <AddressBook/AddressBook.h> #import <AddressBookUI/AddressBookUI.h>
授权
关于通讯录的授权这里不再多说了,我在上一篇中有提到:通讯录授权
访问通讯录
跳转到系统通讯录
- (void)jumpAddress{ ABPeoplePickerNavigationController *peoplePickVc = [[ABPeoplePickerNavigationController alloc] init]; peoplePickVc.peoplePickerDelegate = self; [self presentViewController:peoplePickVc animated:YES completion:nil]; }
添加代理
@interface ViewController ()<ABPeoplePickerNavigationControllerDelegate, UINavigationControllerDelegate>
代理方法
///取消选择 - (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{ }
///实现此方法后,下面的代理方法不会实现(此方法是点击通讯录列表中名字直接调用并dismiss) - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person { //获取当前联系人名字 NSString *firstName=(__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty)); //获取当前联系人姓氏 NSString *lastName=(__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty)); //获取当前联系人的电话 数组 NSMutableArray *phoneArray = [[NSMutableArray alloc]init]; ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty); for (NSInteger j=0; j<ABMultiValueGetCount(phones); j++) { NSString *phone = (__bridge NSString *)(ABMultiValueCopyValueAtIndex(phones, j)); NSLog(@"phone=%@", phone); [phoneArray addObject:phone]; } //获取当前联系人的邮箱 注意是数组 NSMutableArray *emailArray = [[NSMutableArray alloc]init]; ABMultiValueRef emails= ABRecordCopyValue(person, kABPersonEmailProperty); for (NSInteger j=0; j<ABMultiValueGetCount(emails); j++) { NSString *email = (__bridge NSString *)(ABMultiValueCopyValueAtIndex(emails, j)); NSLog(@"email=%@", email); [emailArray addObject:email]; } //获取当前联系人中间名 NSString *middleName=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonMiddleNameProperty)); //获取当前联系人的名字前缀 NSString *prefix=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonPrefixProperty)); //获取当前联系人的名字后缀 NSString *suffix=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonSuffixProperty)); //获取当前联系人的昵称 NSString *nickName=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonNicknameProperty)); //获取当前联系人的名字拼音 NSString *firstNamePhoneic=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonFirstNamePhoneticProperty)); //获取当前联系人的姓氏拼音 NSString *lastNamePhoneic=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonLastNamePhoneticProperty)); //获取当前联系人的中间名拼音 NSString *middleNamePhoneic=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonMiddleNamePhoneticProperty)); //获取当前联系人的公司 NSString *organization=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonOrganizationProperty)); //获取当前联系人的职位 NSString *job=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonJobTitleProperty)); //获取当前联系人的部门 NSString *department=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonDepartmentProperty)); //获取当前联系人的生日 // NSString *birthday=(__bridge NSDate*)(ABRecordCopyValue(person, kABPersonBirthdayProperty)); //获取当前联系人的备注 NSString *notes=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonNoteProperty)); //获取创建当前联系人的时间 注意是NSDate NSDate *creatTime=(__bridge NSDate*)(ABRecordCopyValue(person, kABPersonCreationDateProperty)); //获取最近修改当前联系人的时间 NSDate *alterTime=(__bridge NSDate*)(ABRecordCopyValue(person, kABPersonModificationDateProperty)); //获取当前联系人头像图片 NSData *userImage=(__bridge NSData*)(ABPersonCopyImageData(person)); //获取当前联系人纪念日 NSMutableArray *dateArr = [[NSMutableArray alloc]init]; ABMultiValueRef dates= ABRecordCopyValue(person, kABPersonDateProperty); for (NSInteger j=0; j<ABMultiValueGetCount(dates); j++) { //获取纪念日日期 NSDate *data =(__bridge NSDate*)(ABMultiValueCopyValueAtIndex(dates, j)); //获取纪念日名称 NSString *str =(__bridge NSString*)(ABMultiValueCopyLabelAtIndex(dates, j)); } }
///实现此方法后,上面的代理方法不会实现(此方法是进入通讯录详情后点击名称或电话等调用并dismiss) - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier NS_AVAILABLE_IOS(8_0){ }
注:上面两个选择回调代理实现一个就可以
相关借鉴:http://www.jb51.net/article/95500.htm
以上是关于android开发怎样获取通讯录联系人信息的主要内容,如果未能解决你的问题,请参考以下文章