package com.example.asynctasktest;
import java.io.InputStream;
import java.util.ArrayList;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.AsyncTask;
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.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
/**
* 当执行的操作比较耗时时候,比如查询通讯录数据库。可以使用AsyncTask 这个类去异步查询
* ,一边查询数据一边更新数据。 可以读取十条联系人,更新一次画面。不用等到全部查询结束再去更新画面。
* 不要忘记在AndroidManifest中添加读取联系人的权限
* <!-- 读取联系人权限 -->
<uses-permission android:name="android.permission.READ_CONTACTS"/>
* */
public class ContactView extends Activity
{
ContentResolver resolver;
Cursor phoneCursor;
//定义一个异步查询数据库的任务
AsynLoadcontactTask task;
private Context mContext = null;
private MyListAdapter myAdapter = null;
//查询联系人数据库里的字段
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;
private static final int PHONES_PHOTO_ID_INDEX = 2;
private static final int PHONES_CONTACT_ID_INDEX = 3;
//存放联系人的名字
private ArrayList<String> mContactsName = new ArrayList<String>();
//存放联系人的号码
private ArrayList<String> mContactsNumber = new ArrayList<String>();
//存放联系人的头像,手机中存储的联系人可以有头像,sim卡中一律没有头像
private ArrayList<Bitmap> mContactsPhonto = new ArrayList<Bitmap>();
//存放联系人头像的地址
private ArrayList<Uri> mContactsPhontoUri = new ArrayList<Uri>();
//定义一个listview
private ListView listView = null;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
listView = new ListView(this);
setContentView(listView);
mContext = this;
listView.setBackgroundColor(Color.WHITE);
myAdapter = new MyListAdapter(mContext);
task = new AsynLoadcontactTask();
//异步查询数据库 开始执行
task.execute(0);
}
@Override
public void onBackPressed() {
//取消异步查询
task.cancel(true);
super.onBackPressed();
}
@Override
protected void onResume() {
super.onResume();
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Log.i("xxx", "you clicked item "+arg2);
}
});
listView.setAdapter(myAdapter);
}
//定义一个ViewHolder
private static class ViewHolder {
ImageView image = null;
TextView name = null;
TextView number = null;
}
//adapter 填充listview
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) {
ViewHolder viewHolder = null;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(
R.layout.contactor, null);
viewHolder = new ViewHolder();
viewHolder.image = (ImageView) convertView
.findViewById(R.id.head_icon);
viewHolder.name = (TextView) convertView
.findViewById(R.id.name);
viewHolder.number = (TextView) convertView
.findViewById(R.id.number);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.name.setText(mContactsName.get(position));
viewHolder.number.setText(mContactsNumber.get(position));
viewHolder.image.setImageBitmap(mContactsPhonto.get(position));
return convertView;
}
}
//AsyncTask 查询数据库 边查边去填充ListView, 查询10条联系人 更新一次ListView里的数据。
private class AsynLoadcontactTask extends AsyncTask<Integer, Integer, Integer> {
private AsynLoadcontactTask(){
}
@Override
protected void onPostExecute(Integer result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
phoneCursor.close();
}
protected synchronized Integer doInBackground(Integer... params) {
mContactsName.clear();
mContactsNumber.clear();
mContactsPhonto.clear();
mContactsPhontoUri.clear();
resolver = mContext.getContentResolver();
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;
Long contactid = phoneCursor.getLong(PHONES_CONTACT_ID_INDEX);
String contactName = phoneCursor.getString(PHONES_DISPLAY_NAME_INDEX);
Long photoid = phoneCursor.getLong(PHONES_PHOTO_ID_INDEX);
Bitmap contactPhoto = null;
Uri uri = null;
if(photoid > 0 ) {
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);
}
mContactsName.add(contactName);
mContactsNumber.add(phoneNumber);
mContactsPhonto.add(contactPhoto);
mContactsPhontoUri.add(uri);
//update UI every 10 contacts were read
if (mContactsNumber.size() % 10 == 0) {
//用publishProgress()方法 可以调用 onProgressUpdate(Integer... values) 这个方法
publishProgress(3);
}
}
}
//update all contacts
publishProgress(3);
return null;
}
protected void onPreExecute() {
}
protected void onProgressUpdate(Integer... values) {
myAdapter.notifyDataSetChanged();
}
}
}