![](/img/trans.png)
[英]How can i read names phone numbers and email addresses of all contacts in android
[英]how can I get phone numbers of corresponding names in my contacts?
我的 Cursor 查詢中的這個選擇子句只返回那些有電話號碼的聯系人,這就是我想要的:
// we only want contacts that have a name and a phone number. If they have a phone number, the value is 1 (if not, it is 0)
ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" + ("1") + "'" + " AND " + ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1",
我的整個游標查詢如下所示:
// this query only return contacts with phone number and is not duplicated
phones = getContentResolver().query(
// the table to query
ContactsContract.Contacts.CONTENT_URI,
// the columns to return
null,
// selection criteria :
// we only want contacts that have a name and a phone number. If they have a phone number, the value is 1 (if not, it is 0)
ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" + ("1") + "'" + " AND " + ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1",
// selection criteria
null,
// display in ascending order
ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
但是我怎樣才能獲得每個聯系人的實際電話號碼? 我可以在上面的代碼中添加一些東西,還是需要開始一個新的 Cursor 查詢?
我認為是后者。
我開始了一個新的 Cursor 查詢作為起點:
phonestwo = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.IN_VISIBLE_GROUP + " = '" + ("1") + "'" + " AND " + ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER + "=1",
null,
null);
但是在 logcat 中,我得到了手機游標有 134 條記錄(正確,我想要什么!)而我的手機游標有 196 條記錄。 簡而言之,我如何獲得與這 134 條記錄對應的電話號碼?
要獲取與聯系人關聯的電話號碼,您需要再次點擊 Contacts Content Provider。
首先向phones
光標詢問聯系人的 id -
String phoneContactId = phones.getString(phones.getColumnIndexOrThrow(BaseColumns._ID));
然后對於每個phoneContactId
,您獲取所有關聯的電話號碼 -
Cursor pCur = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { phoneContactId }, null);
while (pCur.moveToNext()) {
int phoneType = pCur.getInt(pCur.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.TYPE));
String phoneNumber = pCur
.getString(pCur.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
public class MainActivity extends Activity {
Cursor cursor;
ListView mainListView;
ArrayList hashMapsArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (cursor != null) {
cursor.moveToFirst();}
try {
cursor = getApplicationContext().getContentResolver()
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
int Idx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID);
int nameIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int phoneNumberIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int photoIdIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_THUMBNAIL_URI);
cursor.moveToFirst();
Set<String> ids = new HashSet<>();
do {
System.out.println("=====>in while");
String contactid=cursor.getString(Idx);
if (!ids.contains(contactid)) {
ids.add(contactid);
HashMap<String, String> hashMap = new HashMap<String, String>();
String name = cursor.getString(nameIdx);
String phoneNumber = cursor.getString(phoneNumberIdx);
String image = cursor.getString(photoIdIdx);
System.out.println("Id--->"+contactid+"Name--->"+name);
System.out.println("Id--->"+contactid+"Name--->"+name);
System.out.println("Id--->"+contactid+"Number--->"+phoneNumber);
if (!phoneNumber.contains("*")) {
hashMap.put("contactid", "" + contactid);
hashMap.put("name", "" + name);
hashMap.put("phoneNumber", "" + phoneNumber);
hashMap.put("image", "" + image);
// hashMap.put("email", ""+email);
if (hashMapsArrayList != null) {
hashMapsArrayList.add(hashMap);}
// hashMapsArrayList.add(hashMap);
}
}
} while (cursor.moveToNext());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.