gpt4 book ai didi

java - 如何使用 sdk 27​​ 在 android studio 中显示联系人电话列表

转载 作者:行者123 更新时间:2023-12-01 17:46:40 24 4
gpt4 key购买 nike

我有代码java android studio来显示hp中的联系人列表,并且已获得许可,但它在sdk> 27中不起作用(空白值)有人帮助我吗?这是我的代码

    contactModelArrayList = new ArrayList<>();
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'",
null,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" COLLATE LOCALIZED ASC");
while (phones.moveToNext())
{
String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

ContactModel contactModel = new ContactModel();
contactModel.setName(name);
contactModel.setNumber(phoneNumber);

contactModelArrayList.add(contactModel);


Log.d("name>>",name+" "+phoneNumber);
}
Log.d("phones", phones.toString());
phones.close();

contactAllAdapter = new ContactAllAdapter(this,contactModelArrayList);
listView.setAdapter(contactAllAdapter);

在 logcat 中什么也没有

最佳答案

像这样创建您的MyCircleContactListData

public class MyCircleContactListData {
@SerializedName("name")
@Expose
public String name;
@SerializedName("img_url")
@Expose
public String imgUrl;

@SerializedName("mobile_no")
@Expose
public String mobileNumber;
}

然后创建联系人访问 Java 类,其中包含联系人列表返回类型方法 getContacts,该方法返回您的联系人列表。

 public class GetContactFromDevice {
private static final String TAG = "GetContactFromDevice";

public ArrayList<MyCircleContactListData> getContacts(Context context) {
ArrayList<MyCircleContactListData> list = new ArrayList<>();
ContentResolver contentResolver = context.getContentResolver();
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
if (cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
Cursor cursorInfo = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(),
ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(id)));

Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(id));
Uri pURI = Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);

Bitmap photo = null;
if (inputStream != null) {
photo = BitmapFactory.decodeStream(inputStream);
}
while (cursorInfo.moveToNext()) {

MyCircleContactListData info = new MyCircleContactListData("", "", "", "", false);
info.name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
info.mobileNumber = TextUtils.validatePhoneNumber(cursorInfo.getString(cursorInfo.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
info.imgUrl= pURI.toString();
list.add(info);

Log.d("GetContactFromDevice", "getContacts: " + info.name);
Log.d("GetContactFromDevice", "getContacts: " + info.mobileNumber);
}

cursorInfo.close();
}
}
cursor.close();
}
return list;
}

}

不要忘记提供联系人访问权限

<uses-permission android:name="android.permission.READ_CONTACTS" />

然后显示您的联系人列表。

GetContactFromDevice getContactFromDevice = new GetContactFromDevice();
contactAllAdapter = new ContactAllAdapter(this,getContactFromDevice.getContacts(this));
listView.setAdapter(contactAllAdapter);

关于java - 如何使用 sdk 27​​ 在 android studio 中显示联系人电话列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54400056/

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