如何使用 SDK 27 在 android studio 中显示联系人列表 phone

how to show list contact phone in android studio with sdk 27

我有代码 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 class

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

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

然后创建 联系人访问 Java Class 使用联系人列表 return 类型方法 getContacts which returns 列表你的联系人。

 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);

你可以试试LoaderManager

创建以下字段

private static final int CONTACTS_LOADER_ID = 101;

private static final String[] PROJECTION = {
        ContactsContract.Contacts._ID,
        ContactsContract.Contacts.DISPLAY_NAME_PRIMARY,
        ContactsContract.CommonDataKinds.Phone.NUMBER
};

创建以下内容class

public class Contact {

    private String id;
    private String name;
    private String number;

    public Contact(String id, String name, String number) {
        this.id = id;
        this.name = name;
        this.number = number;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

}

[1] 创建 LoaderManager.LoaderCallback

的实例
private LoaderManager.LoaderCallbacks<Cursor> loaderCallbacks = new LoaderManager.LoaderCallbacks<Cursor>() {
        @Override
        public Loader<Cursor> onCreateLoader(int id, Bundle args) {

            switch (id) {
                case CONTACTS_LOADER_ID:

                    return new CursorLoader(
                            getActivity(),
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            PROJECTION,
                            null,
                            null,
                            ContactsContract.Contacts.DISPLAY_NAME_PRIMARY + " ASC"
                    );
                default:
                    return null;
            }
        }

        @Override
        public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
            switch (loader.getId()) {
                case CONTACTS_LOADER_ID:

                   //Use ContactUtils class here


                    break;
            }
        }

        @Override
        public void onLoaderReset(Loader<Cursor> loader) {

        }
    };

[2] 使用下面的class将游标数据转换为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 {

    private ContactUtils() {
    }

    public static List<Contact> parseContacts(Cursor cursor) {
        List<Contact> contacts = new ArrayList<>();

        if (cursor.getCount() == 0) {
            return contacts;
        }

        int nameColumnIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY);
        int idColumnIndex = cursor.getColumnIndex(ContactsContract.Contacts._ID);
        int numberColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

        while (cursor.moveToNext()) {
            String contactName = cursor.getString(nameColumnIndex);
            String id = cursor.getString(idColumnIndex);
            String number = cursor.getString(numberColumnIndex);

            Contact contact = new Contact(id, contactName, number);
            if(!isContactAddedInList(contact, contacts)){
                contacts.add(contact);
            }

        }

        return contacts;
    }

public boolean isContactAddedInList(Contact contact, List<Contact> contacts) {

    for (Contact listContact : contacts
            ) {
        if (listContact.number.equals(contact.name)) {
            return true;
        }
    }

    return false;
}


}

[3] 调用 getLoaderManager().initLoader 方法在您的片段中加载联系人或 Activity 的 onStart 方法

getLoaderManager().initLoader(CONTACTS_LOADER_ID, null, loaderCallbacks);
  • 在 menifest 文件中添加 <uses-permission android:name="android.permission.READ_CONTACTS" /> 权限
  • 调用前需要请求运行权限getLoaderManager().initLoader