gpt4 book ai didi

android - 如何获取所有联系人的名字,姓氏,电子邮件,电话号码等而不重复

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:00:14 27 4
gpt4 key购买 nike

我正在尝试使用以下代码获取电话联系人中所有可用联系人的详细信息。但面临重复值的小问题。

已编辑

实际代码开始:-

private String refreshData() {
String emaildata = "";
try {
ContentResolver cr = getBaseContext().getContentResolver();
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP
+ " = '" + ("1") + "'";
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC";
Cursor cur = cr
.query(ContactsContract.Contacts.CONTENT_URI,
null,
selection
+ " AND "
+ ContactsContract.Contacts.HAS_PHONE_NUMBER
+ "=1", null, sortOrder);

if (cur.getCount() > 0) {
Log.i("Content provider", "Reading contact emails");
while (cur.moveToNext()) {
mContactSet.add(cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID)));
}
} else {
emaildata += "Data not found.";
}
cur.close();

Log.i(TAG, "Total contacts = " + mContactSet.size());
Iterator<String> iterator = mContactSet.iterator();

while (iterator.hasNext()) {
String contactId = iterator.next();
Log.i(TAG, "ID ==> " + contactId);

// Create query to use CommonDataKinds classes to fetch
// emails
Cursor emails = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = " + contactId, null, null);

// Name
String whereName = ContactsContract.Data.MIMETYPE
+ " = ? AND "
+ ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID
+ " = ?";
String[] whereNameParams = new String[] {
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE,
contactId };
Cursor nameCur = cr
.query(ContactsContract.Data.CONTENT_URI,
null,
whereName,
whereNameParams,
ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
while (nameCur.moveToNext()) {
String given = nameCur
.getString(nameCur
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
String family = nameCur
.getString(nameCur
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
String display = nameCur
.getString(nameCur
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME));

Log.i(TAG, "First Name ==> " + given);
Log.i(TAG, "Last Name ==> " + family);
Log.i(TAG, "Display ==> " + display);
}
nameCur.close();

}
} catch (Exception e) {
emaildata += "Exception : " + e + "";
}
return emaildata;
}

修改了查询并获得了一些更好的结果,但对于某些联系人和获取重复值来说问题仍然相同。

更新:- 我已经使用 HashSet 获取唯一的联系人 ID,我也成功获取了它,但是当我从联系人 ID 获取姓名时,我得到了一些相同的值 2-3 次联系人。我很困惑,同一个联系人怎么可能用同一个 ID 存储 2-3 次?

我需要为名字、姓氏、电话号码、电子邮件等使用哈希集吗?还有其他方法吗?

最佳答案

这是完整的解决方案

public ArrayList<HashMap<String, Object>> getContacts() {

ArrayList<HashMap<String, Object>> contacts = new ArrayList<HashMap<String, Object>>();
final String[] projection = new String[] { RawContacts.CONTACT_ID, RawContacts.DELETED };

@SuppressWarnings("deprecation")
final Cursor rawContacts = managedQuery(RawContacts.CONTENT_URI, projection, null, null, null);

final int contactIdColumnIndex = rawContacts.getColumnIndex(RawContacts.CONTACT_ID);
final int deletedColumnIndex = rawContacts.getColumnIndex(RawContacts.DELETED);

if (rawContacts.moveToFirst()) {
while (!rawContacts.isAfterLast()) {
final int contactId = rawContacts.getInt(contactIdColumnIndex);
final boolean deleted = (rawContacts.getInt(deletedColumnIndex) == 1);

if (!deleted) {
HashMap<String, Object> contactInfo = new HashMap<String, Object>() {
{
put("contactId", "");
put("name", "");
put("email", "");
put("address", "");
put("photo", "");
put("phone", "");
}
};
contactInfo.put("contactId", "" + contactId);
contactInfo.put("name", getName(contactId));
contactInfo.put("email", getEmail(contactId));
contactInfo.put("photo", getPhoto(contactId) != null ? getPhoto(contactId) : "");
contactInfo.put("address", getAddress(contactId));
contactInfo.put("phone", getPhoneNumber(contactId));
contactInfo.put("isChecked", "false");
contacts.add(contactInfo);
}
rawContacts.moveToNext();
}
}

rawContacts.close();

return contacts;
}

private String getName(int contactId) {
String name = "";
final String[] projection = new String[] { Contacts.DISPLAY_NAME };

final Cursor contact = managedQuery(Contacts.CONTENT_URI, projection, Contacts._ID + "=?", new String[] { String.valueOf(contactId) }, null);

if (contact.moveToFirst()) {
name = contact.getString(contact.getColumnIndex(Contacts.DISPLAY_NAME));
contact.close();
}
contact.close();
return name;

}

private String getEmail(int contactId) {
String emailStr = "";
final String[] projection = new String[] { Email.DATA, // use
// Email.ADDRESS
// for API-Level
// 11+
Email.TYPE };

final Cursor email = managedQuery(Email.CONTENT_URI, projection, Data.CONTACT_ID + "=?", new String[] { String.valueOf(contactId) }, null);

if (email.moveToFirst()) {
final int contactEmailColumnIndex = email.getColumnIndex(Email.DATA);

while (!email.isAfterLast()) {
emailStr = emailStr + email.getString(contactEmailColumnIndex) + ";";
email.moveToNext();
}
}
email.close();
return emailStr;

}

private Bitmap getPhoto(int contactId) {
Bitmap photo = null;
final String[] projection = new String[] { Contacts.PHOTO_ID };

final Cursor contact = managedQuery(Contacts.CONTENT_URI, projection, Contacts._ID + "=?", new String[] { String.valueOf(contactId) }, null);

if (contact.moveToFirst()) {
final String photoId = contact.getString(contact.getColumnIndex(Contacts.PHOTO_ID));
if (photoId != null) {
photo = getBitmap(photoId);
} else {
photo = null;
}
}
contact.close();

return photo;
}

private Bitmap getBitmap(String photoId) {
final Cursor photo = managedQuery(Data.CONTENT_URI, new String[] { Photo.PHOTO }, Data._ID + "=?", new String[] { photoId }, null);

final Bitmap photoBitmap;
if (photo.moveToFirst()) {
byte[] photoBlob = photo.getBlob(photo.getColumnIndex(Photo.PHOTO));
photoBitmap = BitmapFactory.decodeByteArray(photoBlob, 0, photoBlob.length);
} else {
photoBitmap = null;
}
photo.close();
return photoBitmap;
}

private String getAddress(int contactId) {
String postalData = "";
String addrWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] addrWhereParams = new String[] { String.valueOf(contactId), ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE };

Cursor addrCur = managedQuery(ContactsContract.Data.CONTENT_URI, null, addrWhere, addrWhereParams, null);

if (addrCur.moveToFirst()) {
postalData = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));
}
addrCur.close();
return postalData;
}

private String getPhoneNumber(int contactId) {

String phoneNumber = "";
final String[] projection = new String[] { Phone.NUMBER, Phone.TYPE, };
final Cursor phone = managedQuery(Phone.CONTENT_URI, projection, Data.CONTACT_ID + "=?", new String[] { String.valueOf(contactId) }, null);

if (phone.moveToFirst()) {
final int contactNumberColumnIndex = phone.getColumnIndex(Phone.DATA);

while (!phone.isAfterLast()) {
phoneNumber = phoneNumber + phone.getString(contactNumberColumnIndex) + ";";
phone.moveToNext();
}

}
phone.close();
return phoneNumber;
}

如何使用?

ArrayList<HashMap<String, Object>> contactList = getContacts();
System.out.println("Contact List : " +contactList);

输出:

[
{
phone=992-561-1618;848-807-4440;,
contactId=1,
photo=android.graphics.Bitmap@44f40aa0,
address=Zalavadia Strret
Manavadar, Gujarat 362630
India,
email=birajzalavadia@gmail.com;biraj@tasolglobal.com;,
name=Biraj Zalavadia
},
{
phone=992-511-1418;842-827-4450;,
contactId=2,
photo=android.graphics.Bitmap@44f40aa0,
address=Makadiya Strret
Junagadh, Gujarat 364890
India,
email=niles@gmail.com;niles@tasolglobal.com;,
name=Niles patel
}
.......
]

注意:

如果超过一个,您将得到电话和电子邮件分号 (;) 分隔。

关于android - 如何获取所有联系人的名字,姓氏,电子邮件,电话号码等而不重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21471850/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com