gpt4 book ai didi

java - 如何在 Android 上访问 API 17 的联系人

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

我希望能够在 Android 应用程序上读取联系人信息,但对于低于 23 的 API 似乎无法实现。我打算将其用于 API 17。我怎样才能实现这一目标?

最佳答案

此代码应该在 API5 及更高版本中工作,使用枚举 ContactsContract.CommonDataKinds 您可以访问所有字段。

 ContentResolver resolver = context.getContentResolver();
Cursor contacts = null;
try {
contacts = resolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (contacts.moveToFirst()) {
do {
String contactId = contacts.getString(contacts.getColumnIndex(ContactsContract.Contacts._ID));

Cursor emails = null;
Cursor phones = null;
try {
emails = resolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = " + contactId, null, null);
while (emails.moveToNext()) {
String email = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
object
}
phones = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
while (phones.moveToNext()) {
String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String displayName = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
phoneContact object
}
} finally {
if (emails != null) {
emails.close();
}
if (phones != null) {
phones.close();
}
}

} while (contacts.moveToNext());
}
} finally {
if (contacts != null) {
contacts.close();
}
}

关于java - 如何在 Android 上访问 API 17 的联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56376636/

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