Android系統1.6中 讀取聯系人:
- cursor = cr.query(People.CONTENT_URI, null, null, null, "name desc"); //取得聯系人名次
- name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
- //取得聯系人id
- String id = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup._ID));
- //構造查詢條件,根據聯系人id為條件,查詢手機號字段的信息
- String selection = ContactsContract.CommonDataKinds.Phone.CONTACT_ID+" = ? and "+ContactsContract.CommonDataKinds.Phone.TYPE+" = ?";
- String[] selectionArgs = {id,"2"};
- cursorSingle = cr.query(Uri.parse("content://com.android.contacts/data/phones"), new String[]{"data1"}, selection, selectionArgs, null);
- cursorSingle.moveToNext();
- mobile = cursorSingle.getString(0);</PRE>
獲取聯系人名稱和手機號
- cursor.moveToNext();
- name = cursor.getString(cursor.getColumnIndex(Phones.NAME));
- mobile = cursor.getString(cursor.getColumnIndex(Phones.NUMBER));
android系統2.2中 讀取聯系人,此時只是取到名稱以及聯系人的id,還要根據id獲取手機號碼
- cursor = cr.query("content://com.android.contacts/contacts", null, null, null, "sort_key asc");
獲取聯系人名稱和手機號碼
- //取得聯系人名次
- name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
- //取得聯系人id
- String id = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup._ID));
- //構造查詢條件,根據聯系人id為條件,查詢手機號字段的信息
- String selection = ContactsContract.CommonDataKinds.Phone.CONTACT_ID+" = ? and "+ContactsContract.CommonDataKinds.Phone.TYPE+" = ?";
- String[] selectionArgs = {id,"2"};
- cursorSingle = cr.query(Uri.parse("content://com.android.contacts/data/phones"), new String[]{"data1"}, selection, selectionArgs, null);
- cursorSingle.moveToNext();
- mobile = cursorSingle.getString(0);