当前位置: 代码迷 >> Android >> android2.1获得通讯录联系人名字和电话号码
  详细解决方案

android2.1获得通讯录联系人名字和电话号码

热度:16   发布时间:2016-05-01 18:45:56.0
android2.1取得通讯录联系人名字和电话号码

??? 当我们将Andorid1.5及其以前的项目放到Android2.0上时,如果代码中有

import android.provider.Contacts;

?

??? Eclipse会提示“建议不使用”,那是因为在Android2.0中,联系人api发生了变化,需要使用ContactsContract。

?

??? 直接看下面一个最简单的例子,读取联系人的姓名和电话号码:

读取联系人的名字很简单,但是在读取电话号码时,就需要先去的联系人的ID,然后在通过ID去查找电话号码!一个联系人可能存在多个电话号码!

// 取得ContentResolver对象              ContentResolver cr = getContentResolver();                // 取得通讯录的光标              String orderBy = PhoneLookup.DISPLAY_NAME + " COLLATE LOCALIZED ASC";           Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, orderBy);                // 遍历通讯录           ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();           for(int i=0; i<cursor.getCount() ;i++)           {               HashMap<String, Object> map = new HashMap<String, Object>();                              cursor.moveToPosition(i);                              // No.               map.put(COLUMN_ID, i + 1);                              // 取得联系人名字                  int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);                  String name = cursor.getString(nameFieldColumnIndex);                map.put(COLUMN_NAME, name);                              // 取得联系人ID                  String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));                  Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,                       null,                       ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,                       null, null);                               String number = "";               // 取得电话号码(可能存在多个号码)                  for(int j = 0; j < phone.getCount(); j++)                 {                      phone.moveToPosition(j);                   String strPhoneNumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));                      if (j > 0) {                       number += " , ";                     }                   number += strPhoneNumber;                }                  map.put(COLUMN_NUMBER, number);               Log.d(TAG, "number = " + number);               phone.close();                listItem.add(map);                 // Cursor emails = getContentResolver().query(                // ContactsContract.CommonDataKinds.Email.CONTENT_URI,                // null,                // ContactsContract.CommonDataKinds.Email.CONTACT_ID                // + " = " + contactId, null, null);                // while (emails.moveToNext()) {                // String emailAddress = emails                // .getString(emails                // .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));                // System.out.println("emailAddress:" + emailAddress);                // }                // emails.close();        }           cursor.close();                      // 生成适配器的Item和动态数组对应的元素           SimpleAdapter listItemAdapter = new SimpleAdapter(this,                   listItem,// 数据源                   R.layout.list_item,// ListItem的XML实现                   // 动态数组与ListItem对应的子项                   new String[] { COLUMN_ID, COLUMN_NAME, COLUMN_NUMBER },                      new int[] { R.id.TextView1, R.id.TextView2, R.id.TextView3 });           lv.setAdapter(listItemAdapter);//lv为Listview            lv.setOnItemSelectedListener( new AdapterView.OnItemSelectedListener() {        	public void onItemSelected(AdapterView<?> arg0,View arg1,int arg2,long arg3){        		DisplayToast("滚动到第"+Long.toString(arg0.getSelectedItemId())+"项");        	}        	public void onNothingSelected(AdapterView<?> arg0){        		        	}		});        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {			@Override			public void onItemClick(AdapterView<?> arg0, View arg1,					int arg2, long arg3) {				DisplayToast("选中了第"+Integer.toString(arg2+1)+"项");							}		});     }  
??? public void DisplayToast(String str){??? ?Toast.makeText(this, str, Toast.LENGTH_SHORT).show();??? }

??

  相关解决方案