0

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

4

2 回答 2

4

像这样创建您的MyCircleContactListData

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

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

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

 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);
于 2019-01-29T04:44:30.810 回答
0

你可以试试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
};

创建以下类

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] 使用以下类将游标数据转换为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] 在你的 Fragment 或 Activity 的方法中调用getLoaderManager().initLoader方法来加载联系人onStart

getLoaderManager().initLoader(CONTACTS_LOADER_ID, null, loaderCallbacks);
  • 在清单文件中添加 <uses-permission android:name="android.permission.READ_CONTACTS" />权限
  • 您需要在调用之前请求运行时权限 getLoaderManager().initLoader
于 2019-01-29T05:03:59.867 回答