猿问

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

我有代码 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 中什么都没有


慕的地6264312
浏览 225回答 2
2回答

蛊毒传说

像这样创建您的MyCircleContactListData类public class MyCircleContactListData {&nbsp; &nbsp; @SerializedName("name")&nbsp; &nbsp; @Expose&nbsp; &nbsp; public String name;&nbsp; &nbsp; @SerializedName("img_url")&nbsp; &nbsp; @Expose&nbsp; &nbsp; public String imgUrl;&nbsp; &nbsp; @SerializedName("mobile_no")&nbsp; &nbsp; @Expose&nbsp; &nbsp; public String mobileNumber;}然后使用联系人列表返回类型方法 getContacts 创建联系人访问Java 类,该方法返回您的联系人列表。&nbsp;public class GetContactFromDevice {&nbsp; &nbsp; private static final String TAG&nbsp; = "GetContactFromDevice";&nbsp; &nbsp; public ArrayList<MyCircleContactListData> getContacts(Context context) {&nbsp; &nbsp; &nbsp; &nbsp; ArrayList<MyCircleContactListData> list = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; ContentResolver contentResolver = context.getContentResolver();&nbsp; &nbsp; &nbsp; &nbsp; Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);&nbsp; &nbsp; &nbsp; &nbsp; if (cursor.getCount() > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (cursor.moveToNext()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Cursor cursorInfo = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(id)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(id));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Uri pURI = Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Bitmap photo = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (inputStream != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; photo = BitmapFactory.decodeStream(inputStream);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (cursorInfo.moveToNext()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MyCircleContactListData info = new MyCircleContactListData("", "", "", "", false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info.name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info.mobileNumber = TextUtils.validatePhoneNumber(cursorInfo.getString(cursorInfo.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info.imgUrl= pURI.toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add(info);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d("GetContactFromDevice", "getContacts: " + info.name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d("GetContactFromDevice", "getContacts: " + info.mobileNumber);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cursorInfo.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cursor.close();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return list;&nbsp; &nbsp; }}不要忘记提供联系人访问权限<uses-permission android:name="android.permission.READ_CONTACTS" />然后显示您的联系人列表。GetContactFromDevice getContactFromDevice = new GetContactFromDevice();contactAllAdapter = new ContactAllAdapter(this,getContactFromDevice.getContacts(this));listView.setAdapter(contactAllAdapter);

慕娘9325324

你可以试试LoaderManager创建以下字段private static final int CONTACTS_LOADER_ID = 101;private static final String[] PROJECTION = {&nbsp; &nbsp; &nbsp; &nbsp; ContactsContract.Contacts._ID,&nbsp; &nbsp; &nbsp; &nbsp; ContactsContract.Contacts.DISPLAY_NAME_PRIMARY,&nbsp; &nbsp; &nbsp; &nbsp; ContactsContract.CommonDataKinds.Phone.NUMBER};创建以下类public class Contact {&nbsp; &nbsp; private String id;&nbsp; &nbsp; private String name;&nbsp; &nbsp; private String number;&nbsp; &nbsp; public Contact(String id, String name, String number) {&nbsp; &nbsp; &nbsp; &nbsp; this.id = id;&nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; &nbsp; &nbsp; this.number = number;&nbsp; &nbsp; }&nbsp; &nbsp; public String getId() {&nbsp; &nbsp; &nbsp; &nbsp; return id;&nbsp; &nbsp; }&nbsp; &nbsp; public void setId(String id) {&nbsp; &nbsp; &nbsp; &nbsp; this.id = id;&nbsp; &nbsp; }&nbsp; &nbsp; public String getName() {&nbsp; &nbsp; &nbsp; &nbsp; return name;&nbsp; &nbsp; }&nbsp; &nbsp; public void setName(String name) {&nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; }&nbsp; &nbsp; public String getNumber() {&nbsp; &nbsp; &nbsp; &nbsp; return number;&nbsp; &nbsp; }&nbsp; &nbsp; public void setNumber(String number) {&nbsp; &nbsp; &nbsp; &nbsp; this.number = number;&nbsp; &nbsp; }}[1] 创建实例LoaderManager.LoaderCallbackprivate LoaderManager.LoaderCallbacks<Cursor> loaderCallbacks = new LoaderManager.LoaderCallbacks<Cursor>() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public Loader<Cursor> onCreateLoader(int id, Bundle args) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch (id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case CONTACTS_LOADER_ID:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new CursorLoader(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getActivity(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ContactsContract.CommonDataKinds.Phone.CONTENT_URI,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PROJECTION,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; null,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; null,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ContactsContract.Contacts.DISPLAY_NAME_PRIMARY + " ASC"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onLoadFinished(Loader<Cursor> loader, Cursor data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch (loader.getId()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case CONTACTS_LOADER_ID:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//Use ContactUtils class here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onLoaderReset(Loader<Cursor> loader) {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };[2] 使用以下类将游标数据转换为List<Contact>import android.content.ContentResolver;import android.database.Cursor;import android.provider.ContactsContract;import java.util.ArrayList;import java.util.List;public class ContactUtils {&nbsp; &nbsp; private ContactUtils() {&nbsp; &nbsp; }&nbsp; &nbsp; public static List<Contact> parseContacts(Cursor cursor) {&nbsp; &nbsp; &nbsp; &nbsp; List<Contact> contacts = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; if (cursor.getCount() == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return contacts;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; int nameColumnIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY);&nbsp; &nbsp; &nbsp; &nbsp; int idColumnIndex = cursor.getColumnIndex(ContactsContract.Contacts._ID);&nbsp; &nbsp; &nbsp; &nbsp; int numberColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);&nbsp; &nbsp; &nbsp; &nbsp; while (cursor.moveToNext()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String contactName = cursor.getString(nameColumnIndex);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String id = cursor.getString(idColumnIndex);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String number = cursor.getString(numberColumnIndex);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Contact contact = new Contact(id, contactName, number);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(!isContactAddedInList(contact, contacts)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contacts.add(contact);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return contacts;&nbsp; &nbsp; }public boolean isContactAddedInList(Contact contact, List<Contact> contacts) {&nbsp; &nbsp; for (Contact listContact : contacts&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ) {&nbsp; &nbsp; &nbsp; &nbsp; if (listContact.number.equals(contact.name)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return false;}}[3] 在你的 Fragment 或 Activity 的方法中调用getLoaderManager().initLoader方法来加载联系人onStartgetLoaderManager().initLoader(CONTACTS_LOADER_ID, null, loaderCallbacks);在清单文件中添加 <uses-permission android:name="android.permission.READ_CONTACTS" />权限您需要在调用之前请求运行时权限 getLoaderManager().initLoader
随时随地看视频慕课网APP

相关分类

Java
我要回答