使用AsyncTask异步获取联系人信息

来源:互联网 发布:算法第四版高清pdf 编辑:程序博客网 时间:2024/04/30 20:49
Activity源代码:

public class ContactsActivity extends Activity {

private final static StringTAG = "ContactsActivity";

private ListViewlistViewContacts;

private ButtonbuttonConfirm;

private ButtonbuttonCancel;

//保存联系人信息

private ArrayList<HashMap<String,String>>contactsItems = new ArrayList<HashMap<String,String>>();

//列名

private final static String CONTACT_NAME = "name";

private final static String CONTACT_PHONE = "phone";

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.contactsview);

initUI();

//执行异步任务

new GetContactsTask().execute(null);

}

//初始化控件

private void initUI(){

listViewContacts = (ListView) findViewById(R.id.listview_contacts);

buttonConfirm = (Button) findViewById(R.id.btn_contacts_confirm);

buttonCancel = (Button) findViewById(R.id.btn_contacts_cancel);

//设置在ListView为空时显示的View

listViewContacts.setEmptyView(findViewById(R.id.ContactsViewEmpty));

}

//获取联系人信息

private void GetContacts(){

//存储单个联系人信息

HashMap<String, String> mMap;

//获得所有的联系人

    Cursor cur = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);

    //循环遍历

    if (cur.moveToFirst()) {

    int idColumn  = cur.getColumnIndex(ContactsContract.Contacts._ID);

    int displayNameColumn = cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);

    do {

        //获得联系人的ID号

       String contactId = cur.getString(idColumn);

       //获得联系人姓名

       String disPlayName = cur.getString(displayNameColumn);

       //查看该联系人有多少个电话号码。如果没有这返回值为0

       int phoneCount = cur.getInt(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

       if(phoneCount>0){

       //获得联系人的电话号码

      Cursor phones = getContentResolver().query(

ContactsContract.CommonDataKinds.Phone.CONTENT_URI,

null,

ContactsContract.CommonDataKinds.Phone.CONTACT_ID

+" = " + contactId, null, null);

      //获取第一个电话号码

      if(phones.moveToFirst()){

       

      //do{

      //遍历所有的电话号码

      String phoneNumber= phones.getString(phones  

.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

      mMap = new HashMap<String, String>();

     //保存联系人姓名

      mMap.put(CONTACT_NAME, disPlayName);

      //保存联系人第一个电话号码

      mMap.put(CONTACT_PHONE, phoneNumber);

    contactsItems.add(mMap);

     //System.out.println(phoneNumber);

      //}while(phones.moveToNext());

      }      }

            } while (cur.moveToNext());

    }

}

//异步Task

private class GetContactsTaskextends AsyncTask<Void,Void,Void>{


@Override

protected Void doInBackground(Void... params) {

GetContacts();

returnnull;

}


@Override

protectedvoid onPostExecute(Void result) {

listViewContacts.setAdapter(new SimpleAdapter(ContactsActivity.this

contactsItems

R.layout.contacts_item

new String[]{CONTACT_NAME,CONTACT_PHONE}, 

newint[]{R.id.contacts_name,R.id.contacts_phone}));

}

}

}


对应的XML文件:(1)contactsview.xml

<?xml version="1.0"encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical">

<ViewFlipper android:id="@+id/SmsViewFlipper" 

   android:background="#fff" 

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<ListViewandroid:id="@+id/listview_contacts"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent"

  android:divider="#9c9c9c" 

  android:dividerHeight="1dp"

  >

  </ListView>

  <TextView 

    android:id="@+id/ContactsViewEmpty"

    android:textColor="#000"

    android:layout_width="wrap_content"

android:layout_height="wrap_content" 

android:text="@string/nocontacts" 

android:layout_gravity="center">

</TextView>

  </ViewFlipper>

  

  

  <LinearLayout

    android:id = "@+id/layout01"

            android:orientation="vertical"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:background="@drawable/gradient_orange"

            android:layout_alignParentBottom = "true">

                      

                        <LinearLayout

                                android:orientation="horizontal"

                                android:layout_width="fill_parent"

                                android:layout_height="50dp"

                                android:gravity="center_vertical"

                                >

                               

                                <Button

                        android:id="@+id/btn_contacts_confirm"

                        android:layout_weight="1"

                        android:layout_width="fill_parent"

                       android:layout_height="50dp"

                        android:text="@string/confirm"

                        >

                </Button>

                                

                                <Button

                        android:id="@+id/btn_contacts_cancel"

                        android:layout_weight="1"

                        android:layout_width="fill_parent"

                       android:layout_height="50dp"

                        android:text="@string/cancel"

                        android:layout_toRightOf="@+id/btn_contacts_confirm"

                        >

                </Button>

                        </LinearLayout>   

                                                   

                </LinearLayout>

</RelativeLayout>

(2)contacts_item.xml

<?xml version="1.0"encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical">

<LinearLayout

  android:id="@+id/contacts_linearlayout"

  android:orientation="vertical" 

  android:layout_width="fill_parent" 

  android:layout_height="wrap_content">

  <TextView

  android:id="@+id/contacts_name" 

  style="@style/textviewcontactsnamestyles"


 

  ></TextView>

  <TextView

  android:id="@+id/contacts_phone" 

  style="@style/textviewcontactsphonestyles"

 

  ></TextView>

</LinearLayout>

<CheckBox

  android:id="@+id/contacts_checkbox"

  android:layout_alignParentRight="true"

  android:layout_height="wrap_content"

  android:layout_width="wrap_content"></CheckBox>

</RelativeLayout>

转自:http://blog.163.com/supered_yang@126/blog/static/4126004120120565054340/

0 0
原创粉丝点击