gpt4 book ai didi

java - 读取联系人仅显示电子邮件存储中的联系人

转载 作者:行者123 更新时间:2023-12-02 02:26:44 25 4
gpt4 key购买 nike

我想创建一个功能来读取我的联系人与 SIM 1 和 2 联系人、电子邮件存储中的联系人以及手机存储中的联系人的所有联系人号码。但是,在我的应用程序中,仅显示电子邮件存储中的联系人。请有人帮助我。

这是我获取联系人的代码。

public class ContactUtils {
//get contacts from phonebook
public static List<PhoneContact> getRawContacts(Context context) {
List<PhoneContact> contactsList = new ArrayList<>();

Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection = new String[]{
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
};

String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

// Build adapter with contact entries
Cursor mCursor = null;
Cursor phoneNumCursor = null;

try {
mCursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

while (mCursor.moveToNext()) {
//get contact name
String name = mCursor.getString(mCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

//get contact name
String contactID = mCursor.getString(mCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
//create new phoneContact object
PhoneContact contact = new PhoneContact();
contact.setId(Integer.parseInt(contactID));
contact.setName(name);


//get all phone numbers in this contact if it has multiple numbers
phoneNumCursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?", new String[]{contactID}, null);

phoneNumCursor.moveToFirst();


//create empty list to fill it with phone numbers for this contact
List<String> phoneNumberList = new ArrayList<>();


while (!phoneNumCursor.isAfterLast()) {
//get phone number
String number = phoneNumCursor.getString(phoneNumCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));


//prevent duplicates numbers
if (!phoneNumberList.contains(number))
phoneNumberList.add(number);

phoneNumCursor.moveToNext();
}

//fill contact object with phone numbers
contact.setPhoneNumbers(phoneNumberList);
//add final phoneContact object to contactList
contactsList.add(contact);

}

mCursor.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (mCursor != null)
mCursor.close();
if (phoneNumCursor != null)
phoneNumCursor.close();
}

return contactsList;

}


//format number to international number
//if number is not with international code (+1 for example) we will add it
//depending on user country ,so if the user number is +1 1234-111-11
//we will add +1 in this case for all the numbers
//and if it's contains "-" we will remove them
private static String formatNumber(Context context, String countryCode, String number) {

PhoneNumberUtil util = PhoneNumberUtil.createInstance(context);
Phonenumber.PhoneNumber phoneNumber;
String phone = number;
try {
//format number depending on user's country code
phoneNumber = util.parse(number, countryCode);
phone = util.format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL);

} catch (NumberParseException e) {
e.printStackTrace();
}

//remove empty spaces and dashes
if (phone != null)
phone = phone.replaceAll(" ", "")
.replaceAll("-", "")
.replaceAll("\\(","")
.replaceAll("\\)","");

return phone;


}

//get the Contact name from phonebook by number
public static String queryForNameByNumber(Context context, String phone) {
String name = phone;

try {
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone));

String[] projection = new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME};

Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null, null);

if (cursor != null) {
if (cursor.moveToFirst()) {
name = cursor.getString(0);
}
cursor.close();
}
} catch (Exception e) {
return name;
}
return name;

}

我将显示我的联系人列表中的所有联系人,而不仅仅是电子邮件存储中的

最佳答案

  ContentResolver cr = context.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String phone="";
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
//Query phone here. Covered next
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
// Do something with phones
phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
pCur.close();
}

关于java - 读取联系人仅显示电子邮件存储中的联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57248844/

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