Android reads the phone address book and displays listview

Source: Internet
Author: User

Android reads the phone address book and displays listview
When I register an account, I have enabled the permission. 1. If the address book is empty, I will not allow you to proceed. 2. If you have not enabled the permission, 3. If the permission is enabled and the address book is empty, you are not allowed to proceed. 4. If the permission is enabled and the address book is not empty, you can proceed. Read Address Book Permissions

     
     
      
  
 


Key code
/** Contact name **/private ArrayList
 
  
MContacts = new ArrayList
  
   
(); Private static final String [] PHONES_PROJECTION = new String [] {Phone. DISPLAY_NAME, Phone. NUMBER, Phone. PHOTO_ID, Phone. CONTACT_ID};/** display contact name **/private static final int PHONES_DISPLAY_NAME_INDEX = 0;/** phone number **/private static final int PHONES_NUMBER_INDEX = 1; /** Avatar ID **/private static final int PHONES_PHOTO_ID_INDEX = 2;/** contact ID **/private static final int PHONES_CONTACT_ID_INDEX = 3; priv Ate void getPhoneContacts () {ContentResolver resolver = getContentResolver (); try {// get the mobile phone contact Cursor phoneCursor = resolver. query (Phone. CONTENT_URI, PHONES_PROJECTION, null); if (phoneCursor! = Null) {while (phoneCursor. moveToNext () {// obtain the mobile phone number String phoneNumber = phoneCursor. getString (PHONES_NUMBER_INDEX); // when the mobile phone number is empty or the field is empty, skip the current loop if (TextUtils. isEmpty (phoneNumber) continue; // obtain the contact name String contactName = phoneCursor. getString (PHONES_DISPLAY_NAME_INDEX); // obtain the contact IDLong contactid = phoneCursor. getLong (PHONES_CONTACT_ID_INDEX); // obtain the Contact Profile IDLong photoid = phoneCursor. getLong (PHONES_PHOTO_ID_INDEX); // obtain the Contact Profile BitampBitmap contactPhoto = null; // if the photoid is greater than 0, the contact has an Avatar. if no Avatar is set for this person, the default if (photoid> 0) {Uri uri = ContentUris is. 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 entity class code:
Package com. yqy. yqy_testtxl; import android. graphics. bitmap;/*** contact information ** @ author YQY **/public class ContactEntity {/** contact name **/private String name; /** contact number **/private String number;/** Contact Profile **/private Bitmap photo; @ Overridepublic 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 ;}}


Displayed in listview:
private void initList() {ListView lv = (ListView) findViewById(R.id.listView1);lv.setAdapter(new MyAdapter());}class MyAdapter extends BaseAdapter {@Overridepublic int getCount() {if (mContacts != null && mContacts.size() > 0) {return mContacts.size();} else {return 0;}}@Overridepublic Object getItem(int position) {if (mContacts != null && mContacts.size() > 0) {return mContacts.get(position);} else {return null;}}@Overridepublic long getItemId(int arg0) {// TODO Auto-generated method stubreturn arg0;}@Overridepublic 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;}}


All MainActivity code:
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 {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); getPhoneContacts (); Log. e (YQY, mContacts. size () + ------------- mContacts ---------); initList () ;}@ Overridepublic 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;}/** contact name **/private ArrayList
 
  
MContacts = new ArrayList
  
   
(); Private static final String [] PHONES_PROJECTION = new String [] {Phone. DISPLAY_NAME, Phone. NUMBER, Phone. PHOTO_ID, Phone. CONTACT_ID};/** display contact name **/private static final int PHONES_DISPLAY_NAME_INDEX = 0;/** phone number **/private static final int PHONES_NUMBER_INDEX = 1; /** Avatar ID **/private static final int PHONES_PHOTO_ID_INDEX = 2;/** contact ID **/private static final int PHONES_CONTACT_ID_INDEX = 3; priv Ate void getPhoneContacts () {ContentResolver resolver = getContentResolver (); try {// get the mobile phone contact Cursor phoneCursor = resolver. query (Phone. CONTENT_URI, PHONES_PROJECTION, null); if (phoneCursor! = Null) {while (phoneCursor. moveToNext () {// obtain the mobile phone number String phoneNumber = phoneCursor. getString (PHONES_NUMBER_INDEX); // when the mobile phone number is empty or the field is empty, skip the current loop if (TextUtils. isEmpty (phoneNumber) continue; // obtain the contact name String contactName = phoneCursor. getString (PHONES_DISPLAY_NAME_INDEX); // obtain the contact IDLong contactid = phoneCursor. getLong (PHONES_CONTACT_ID_INDEX); // obtain the Contact Profile IDLong photoid = phoneCursor. getLong (PHONES_PHOTO_ID_IN DEX); // get the Contact Profile BitampBitmap contactPhoto = null; // if the contact profile is not set for this person, the default if (photoid> 0) is displayed) {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 {@ Overridepublic int getCount () {if (mContacts! = Null & mContacts. size ()> 0) {return mContacts. size () ;}else {return 0 ;}@ Overridepublic Object getItem (int position) {if (mContacts! = Null & mContacts. size ()> 0) {return mContacts. get (position) ;}else {return null ;}@ Overridepublic long getItemId (int arg0) {// TODO Auto-generated method stubreturn arg0 ;} @ Overridepublic 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 ;}}}
  
 


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.