Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 讀取手機通訊錄並顯示listview

android 讀取手機通訊錄並顯示listview

編輯:關於Android編程

脈脈中注冊時有一個,我已開啟權限:

這個原理:

1、如果通訊錄為空,脈脈就不讓你往下進行, 2、如果沒有開啟權限,脈脈就不讓你往下進行, 3、如果開啟權限且通訊錄為空,脈脈就不讓你往下進行, 4、如果開啟權限且通訊錄不為空,脈脈才能讓你往下進行。

 

讀取通訊錄權限


    
    
    


關鍵代碼

/** 聯系人名稱 **/
	private ArrayList mContacts = new ArrayList();

	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 mContacts = new ArrayList();

	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;
		}

	}

}


 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved