android 读取手机通讯录并显示listview
脉脉中注册时有一个,我已开启权限:
这个原理:
读取通讯录权限
<!-- 读取联系人权限 --> <uses-permission android:name="android.permission.READ_CONTACTS" /> <!-- 拨打电话权限 --> <uses-permission android:name="android.permission.CALL_PHONE" />
关键代码
/** 联系人名称 **/ private ArrayList<ContactEntity> mContacts = new ArrayList<ContactEntity>(); private static final String[] PHONES_PROJECTION = new String[] { Phone.DISPLAY_NAME, Phone.NUMBER, Phone.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 void getPhoneContacts() { ContentResolver resolver = getContentResolver(); try { // 获取手机联系人 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.ic_launcher); } ContactEntity mContact = new ContactEntity(contactName, phoneNumber, contactPhoto); mContacts.add(mContact); } phoneCursor.close(); } } catch (Exception e) { e.printStackTrace(); } }
ContactEntity实体类代码:
package com.yqy.yqy_testtxl; import android.graphics.Bitmap; /** * 联系人信息 * @author YQY * */ public class ContactEntity { /** 联系人名称 **/ private String name; /** 联系人号码 **/ private String number; /** 联系人头像 **/ private Bitmap photo; @Override public String toString() { return "ContactEntity [name=" + name + ", number=" + number + ", photo=" + photo + "]"; } public ContactEntity(String name, String number, Bitmap photo) { super(); this.name = name; this.number = number; this.photo = photo; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public Bitmap getPhoto() { return photo; } public void setPhoto(Bitmap photo) { this.photo = photo; } }
显示到listview:
private void initList() { ListView lv = (ListView) findViewById(R.id.listView1); lv.setAdapter(new MyAdapter()); } class MyAdapter extends BaseAdapter { @Override public int getCount() { if (mContacts != null && mContacts.size() > 0) { return mContacts.size(); } else { return 0; } } @Override public Object getItem(int position) { if (mContacts != null && mContacts.size() > 0) { return mContacts.get(position); } else { return null; } } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return arg0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { holder = new ViewHolder(); convertView = LayoutInflater.from(MainActivity.this).inflate( R.layout.item_contact, null); holder.name = (TextView) convertView.findViewById(R.id.tv_name); holder.number = (TextView) convertView .findViewById(R.id.tv_number); holder.photo = (ImageView) convertView .findViewById(R.id.iv_photo); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } ContactEntity contact = mContacts.get(position); holder.name.setText(contact.getName()+""); holder.number.setText(contact.getNumber()+""); holder.photo.setImageBitmap(contact.getPhoto()); return convertView; } class ViewHolder { TextView name; TextView number; ImageView photo; } }
MainActivity全部代码:
package com.yqy.yqy_testtxl; import java.io.InputStream; import java.util.ArrayList; import android.app.Activity; import android.content.ContentResolver; import android.content.ContentUris; 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.text.TextUtils; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getPhoneContacts(); Log.e("YQY", mContacts.size() + "-------------mContacts---------"); initList(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } /** 联系人名称 **/ private ArrayList<ContactEntity> mContacts = new ArrayList<ContactEntity>(); private static final String[] PHONES_PROJECTION = new String[] { Phone.DISPLAY_NAME, Phone.NUMBER, Phone.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 void getPhoneContacts() { ContentResolver resolver = getContentResolver(); try { // 获取手机联系人 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.ic_launcher); } ContactEntity mContact = new ContactEntity(contactName, phoneNumber, contactPhoto); mContacts.add(mContact); } phoneCursor.close(); } } catch (Exception e) { e.printStackTrace(); } } private void initList() { ListView lv = (ListView) findViewById(R.id.listView1); lv.setAdapter(new MyAdapter()); } class MyAdapter extends BaseAdapter { @Override public int getCount() { if (mContacts != null && mContacts.size() > 0) { return mContacts.size(); } else { return 0; } } @Override public Object getItem(int position) { if (mContacts != null && mContacts.size() > 0) { return mContacts.get(position); } else { return null; } } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return arg0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { holder = new ViewHolder(); convertView = LayoutInflater.from(MainActivity.this).inflate( R.layout.item_contact, null); holder.name = (TextView) convertView.findViewById(R.id.tv_name); holder.number = (TextView) convertView .findViewById(R.id.tv_number); holder.photo = (ImageView) convertView .findViewById(R.id.iv_photo); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } ContactEntity contact = mContacts.get(position); holder.name.setText(contact.getName()+""); holder.number.setText(contact.getNumber()+""); holder.photo.setImageBitmap(contact.getPhoto()); return convertView; } class ViewHolder { TextView name; TextView number; ImageView photo; } } }
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" > </ListView> </RelativeLayout>
运行截图就不上了。。。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。