查询 select 个联系人的联系人列表 - 多个 URI Android?
Query on contact list for select contacts - Multiple URI Android?
如何在一个查询中设置对 ContactsContract.CommonDataKinds.Email
和 ContactsContract.CommonDataKinds.Phone
的查询并获取 ID、姓名、Phone 号码和电子邮件....?
喜欢下面的代码:
private void getContactList() {
String id = "";
String name = "";
String phoneNo = "";
String mail = "";
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if ((cur != null ? cur.getCount() : 0) > 0) {
while (cur != null && cur.moveToNext()) {
id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
//if (cur.getInt(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null);
Cursor eCur = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
if (pCur != null) {
while (pCur.moveToNext()) {
phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
if (phoneNo != null && !TextUtils.isEmpty(phoneNo)) {
//Log.i("ASDASDASDASDD", "id: " + id);
Log.i("ASDASDASDASDD", "Name: " + name);
Log.i("ASDASDASDASDD", "Phone Number: " + phoneNo);
Log.i("ASDASDASDASDD", "email: ");
} else {
phoneNo = "";
}
}
pCur.close();
} else {
phoneNo = "";
}
if (eCur != null) {
while (eCur.moveToNext()) {
mail = eCur.getString(eCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
if (mail != null && !TextUtils.isEmpty(mail)) {
//Log.i("ASDASDASDASDD", "id: " + id);
Log.i("ASDASDASDASDD", "Name: " + name);
Log.i("ASDASDASDASDD", "Phone Number: " + phoneNo);
Log.i("ASDASDASDASDD", "email: " + mail);
} else {
mail = "";
}
}
eCur.close();
} else {
mail = "";
}
}
//}
}
if (cur != null) {
cur.close();
}
}
我需要 Phone、姓名、身份证、电子邮件和...。
解决了我的问题:
private void XXXXX() {
StringBuilder phoneNo = new StringBuilder();
StringBuilder email = new StringBuilder();
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if ((cur != null ? cur.getCount() : 0) > 0) {
while (cur != null && cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id, null,
null);
if (phones != null) {
while (phones.moveToNext()) {
phoneNo.append("\n").append(phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
phones.close();
} else {
phoneNo = new StringBuilder();
}
Cursor emailC = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + id, null,
null);
if (emailC != null) {
while (emailC.moveToNext()) {
email.append("\n").append(emailC.getString(emailC.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)));
}
emailC.close();
} else {
email = new StringBuilder();
}
Log.e("ASDASASDAS",
"name : " + name
+ " \n " +
" phone : " + phoneNo
+ " \n " +
" email : " + email
+ " \n");
phoneNo = new StringBuilder();
email = new StringBuilder();
}
}
}
Email
和 Phone
table 实际上是数据 table 的便利 URI,其中包含所有类型的所有数据(电子邮件、电话等) !)
String[] projection = new String[] { Data.CONTACT_ID, Data.DISPLAY_NAME, Data.MIMETYPE, Data.DATA1 };
Cursor cur = getContentResolver().query(Data.CONTENT_URI, projection, Data.MIMETYPE + " IN ('" + Email.CONTENT_ITEM_TYPE + "' , '" + Phone.CONTENT_ITEM_TYPE +"')", null, null);
while ((cur != null) && cur.moveToNext()) {
long contactId = cur.getLong(0);
String name = cur.getString(1);
String type = cur.getString(2);
String data = cur.getString(3);
if (type == Email.CONTENT_ITEM_TYPE) {
Log.e("Data", "Found an email: " + contactId + ", " + name + ", " + type + ", " + data);
} else {
Log.e("Data", "Found a phone: " + contactId + ", " + name + ", " + type + ", " + data);
}
}
如何在一个查询中设置对 ContactsContract.CommonDataKinds.Email
和 ContactsContract.CommonDataKinds.Phone
的查询并获取 ID、姓名、Phone 号码和电子邮件....?
喜欢下面的代码:
private void getContactList() {
String id = "";
String name = "";
String phoneNo = "";
String mail = "";
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if ((cur != null ? cur.getCount() : 0) > 0) {
while (cur != null && cur.moveToNext()) {
id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
//if (cur.getInt(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null);
Cursor eCur = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
if (pCur != null) {
while (pCur.moveToNext()) {
phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
if (phoneNo != null && !TextUtils.isEmpty(phoneNo)) {
//Log.i("ASDASDASDASDD", "id: " + id);
Log.i("ASDASDASDASDD", "Name: " + name);
Log.i("ASDASDASDASDD", "Phone Number: " + phoneNo);
Log.i("ASDASDASDASDD", "email: ");
} else {
phoneNo = "";
}
}
pCur.close();
} else {
phoneNo = "";
}
if (eCur != null) {
while (eCur.moveToNext()) {
mail = eCur.getString(eCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
if (mail != null && !TextUtils.isEmpty(mail)) {
//Log.i("ASDASDASDASDD", "id: " + id);
Log.i("ASDASDASDASDD", "Name: " + name);
Log.i("ASDASDASDASDD", "Phone Number: " + phoneNo);
Log.i("ASDASDASDASDD", "email: " + mail);
} else {
mail = "";
}
}
eCur.close();
} else {
mail = "";
}
}
//}
}
if (cur != null) {
cur.close();
}
}
我需要 Phone、姓名、身份证、电子邮件和...。
解决了我的问题:
private void XXXXX() {
StringBuilder phoneNo = new StringBuilder();
StringBuilder email = new StringBuilder();
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if ((cur != null ? cur.getCount() : 0) > 0) {
while (cur != null && cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id, null,
null);
if (phones != null) {
while (phones.moveToNext()) {
phoneNo.append("\n").append(phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
phones.close();
} else {
phoneNo = new StringBuilder();
}
Cursor emailC = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + id, null,
null);
if (emailC != null) {
while (emailC.moveToNext()) {
email.append("\n").append(emailC.getString(emailC.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)));
}
emailC.close();
} else {
email = new StringBuilder();
}
Log.e("ASDASASDAS",
"name : " + name
+ " \n " +
" phone : " + phoneNo
+ " \n " +
" email : " + email
+ " \n");
phoneNo = new StringBuilder();
email = new StringBuilder();
}
}
}
Email
和 Phone
table 实际上是数据 table 的便利 URI,其中包含所有类型的所有数据(电子邮件、电话等) !)
String[] projection = new String[] { Data.CONTACT_ID, Data.DISPLAY_NAME, Data.MIMETYPE, Data.DATA1 };
Cursor cur = getContentResolver().query(Data.CONTENT_URI, projection, Data.MIMETYPE + " IN ('" + Email.CONTENT_ITEM_TYPE + "' , '" + Phone.CONTENT_ITEM_TYPE +"')", null, null);
while ((cur != null) && cur.moveToNext()) {
long contactId = cur.getLong(0);
String name = cur.getString(1);
String type = cur.getString(2);
String data = cur.getString(3);
if (type == Email.CONTENT_ITEM_TYPE) {
Log.e("Data", "Found an email: " + contactId + ", " + name + ", " + type + ", " + data);
} else {
Log.e("Data", "Found a phone: " + contactId + ", " + name + ", " + type + ", " + data);
}
}