推荐安卓开发神器(里面有各种UI特效和android代码库实例)
一下文章引自网络快照
http://203.208.37.132/search?q=cache:65WdqffxH-UJ:konshi.blogspot.com/2009/12/android-20.html+ContactsContract&cd=16&hl=zh-CN&ct=clnk&gl=cn&inlang=zh-CN&client=aff-avalanche&st_usg=ALhdy2-Kg5_1uawrMLIyiSfUHm1PLeA1GQ
如果使用了
import android.provider.Contacts; import android.provider.Contacts.People;
就要特別注意一下,
因為在android 2.0 SDK當中不建議使用,
請改用
import android.provider.ContactsContract; import android.provider.ContactsContract.PhoneLookup;
要取得聯絡人的姓名sample code如下:
final Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); while(people.moveToNext()) { int nameFieldColumnIndex = people.getColumnIndex(PhoneLookup.DISPLAY_NAME); String contact = people.getString(nameFieldColumnIndex); } people.close();
奇怪的事,如果要取得聯絡人的電話
如果用
int nameFieldColumnIndex = people.getColumnIndex(PhoneLookup.NUMBER); String phone = people.getString(nameFieldColumnIndex); //nameFieldColumnIndex return回來的值總是 ─1,
上網google了一下,找到瞭解法,
1. 首先找到目標聯絡人的 _ID,例如要找第一個聯絡人的_ID
Cursor target = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null); target.moveToFirst(); String contactId = cursor.getString(target.getColumnIndex(ContactsContract.Contacts._ID));
2. 接著透過_ID,來找電話號碼,電話號碼可能不只有一個,所以先判斷有無電話號碼
String IsPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
3. 如果有電話,接著再去query電話號碼
if( (Integer.parseInt(IsPhone) > 0) ) { Cursor phoneNumber = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null); while (phones.moveToNext()) { String strPhoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); } }
** 如果要查詢e-mail,程式如下
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)); }
另外
Java代码
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null); while (cursor.moveToNext()) { String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); if (Boolean.parseBoolean(hasPhone)) { Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null); while (phones.moveToNext()) { String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); } phones.close(); } 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.CommonDataColumns.DATA)); } emails.close(); } cursor.close();
1.5
package com.activity; import android.app.Activity; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.Contacts; import android.provider.Contacts.People; import android.widget.TextView; public class Main extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView tv = (TextView) findViewById(R.id.TextView01); String columns[] = new String[] { People._ID, People.NAME, People.NUMBER, People.PRIMARY_EMAIL_ID, People.PRIMARY_ORGANIZATION_ID, People.PRIMARY_PHONE_ID, People.DISPLAY_NAME, People.IM_ACCOUNT, People.IM_HANDLE, People.PHONETIC_NAME, People.TYPE }; Uri mContacts = People.CONTENT_URI; Cursor cur = managedQuery(mContacts, columns, // 要返回的数据字段 null, // WHERE子句 null, // WHERE 子句的参数 People.NAME // Order-by子句 ); if (cur.moveToFirst()) { Cursor newcur = null; do { // 获取字段的值 String name = cur.getString(cur.getColumnIndex(People.NAME)); String phoneNo = cur.getString(cur .getColumnIndex(People.NUMBER)); String peopleId = cur.getString(cur.getColumnIndex(People._ID)); String[] PROJECTION = new String[] { Contacts.ContactMethods._ID, Contacts.ContactMethods.KIND, Contacts.ContactMethods.DATA }; newcur = managedQuery(Contacts.ContactMethods.CONTENT_URI, PROJECTION, Contacts.ContactMethods.PERSON_ID + "=\'" + cur.getLong(cur.getColumnIndex(People._ID)) + "\'", null, null); startManagingCursor(newcur); String email = ""; if (newcur.moveToFirst()) { while (newcur.moveToNext()) { email = email+ newcur.getString(newcur.getColumnIndex(Contacts.ContactMethods.DATA)); } } tv.setText("name = " + name + " phoneNo = " + phoneNo + "email = " + email); if (email != null && !"".equals(email) && email.trim().length() != 0) { // 此处可以取到联系人邮件 } } while (cur.moveToNext()); if (newcur != null) { newcur.close();// 用完得关闭吧 } } if (cur != null) cur.close(); // 用完得关闭吧 } }