gpt4 book ai didi

android - 如何仅获取在 Android 中附加了 StructuredAddress 的联系人?

转载 作者:行者123 更新时间:2023-11-29 02:11:15 25 4
gpt4 key购买 nike

通过执行以下代码,我可以解析所有 Android 通讯簿项目和附加的邮政地址:

   Cursor cursor = mContext.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor addresses = mContext.getContentResolver().query( ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI, null, ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID +" = "+ contactId, null, null);
while (addresses.moveToNext()) {
String city = addresses.getString(addresses.getColumnIndex( ContactsContract.CommonDataKinds.StructuredPostal.CITY));
// other structured address fields.
}
}

我对无地址条目不感兴趣,有谁知道我是否可以在第一个查询中只选择具有结构化地址的地址簿项目,这样我就不必浪费时间解析所有这些项目?

最佳答案

是的,有。我也一直在寻找相当长的时间来快速和正确地获得它。我想到了这个:

private ArrayList<CustomAddressObject> getPostalAddresses(){
Cursor postals = null;
ArrayList<CustomAddressObject> results = null;
try{

postals = getContentResolver().query(
ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,
null,
null,
null,
null);
int postFormattedNdx = postals.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS);
int postTypeNdx = postals.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE);
int postLabelNdx = postals.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.LABEL);
int postNameNdx = postals.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);

if(postals.getCount() == 0){
//No contacts with addresses
return null;
}
results = new ArrayList<CustomAddressObject>();

while (postals.moveToNext()) {
CustomAddressObject address = new CustomAddressObject ();
address.address = postals.getString(postFormattedNdx);
address.contactName = postals.getString(postNameNdx);
Log.d(TAG, "Contact name: "+address.contactName);
if(address.address != null && address.contactName != null){
switch (postals.getInt(postTypeNdx)) {
case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_CUSTOM:
address.label = postals.getString(postLabelNdx);
if(address.label == null){
address.label = getString(R.string.contacts_type_other);
}
break;
case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME:
address.label = getString(R.string.contacts_type_home);
break;
case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK:
address.label = getString(R.string.contacts_type_work);
break;
case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_OTHER:
address.label = getString(R.string.contacts_type_other);
break;
default:
address.label = null;
}
if(!results.contains(address))
results.add(address);
}else{
//Some phones can store some information inside the postals that don't actually contain an address...
Log.d(TAG, "Contact has no address with id : "+contactId);
Log.d(TAG, "Contact has no address with name: "+getContactName(contactId));
}
}
}
catch (IllegalStateException e) {
// TODO: handle exception
}finally{
if(postals != null && !postals.isClosed()){
postals.close();
}
}
return results;
}

关于android - 如何仅获取在 Android 中附加了 StructuredAddress 的联系人?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7260455/

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