1

我编写了这个函数来检索 android 手机中的联系人:

public static HashMap<String, Contact> getAllContacts(Context context) {

    HashMap<String, Contact> contactsList = new HashMap<>();

    if(!contactsReadPermission(context))
        return contactsList;

    DateTime timestamp = DateTime.now();

    ContentResolver contentResolver = context.getContentResolver();
    Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.Contacts.SORT_KEY_PRIMARY + " ASC");
    if (cursor != null) {
        while (cursor.moveToNext()) {

            String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

            Cursor phoneCursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);

            if (phoneCursor != null) {
                while (phoneCursor.moveToNext()) {

                    String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER));

                    if (phoneNumber == null)
                        continue;

                    contactsList.put(phoneNumber, new Contact(name, phoneNumber, timestamp.toString()));
                }
                phoneCursor.close();
            }
        }
        cursor.close();
    }

    return contactsList;
}

这段代码在除小米手机之外的所有安卓设备上都能正常工作。我认为这可能是 MIUI 设备的问题。

当我在 MIUI ROM 上运行此函数时,它会返回空的哈希映射。

4

2 回答 2

0

我很抱歉浪费了你的时间,以防你花时间在这上面。我将这段代码与 SyncAdapter 一起使用,真正的问题完全是另外一回事,实际上 MIUI 中的 SyncAdapters 不起作用,除非我们在 AppInfo 中执行 autostart=true。

于 2019-01-19T20:13:56.563 回答
0

最好的事情是它会为您提供所有带有国家代码附加的唯一联系人

对于 kotlin,您可以像下面这样使用它

fun fetchContacts(context: Context): Set<ContactModel> {
    val list = HashSet<ContactModel>()
    val startnow: Long = android.os.SystemClock.uptimeMillis()

    val util = PhoneNumberUtil.getInstance()
    val tm = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
    val countryCodeValue = tm.networkCountryIso.toUpperCase()

    val projection = arrayOf(ContactsContract.Data.MIMETYPE, ContactsContract.Data.CONTACT_ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.PHOTO_URI, ContactsContract.Contacts.STARRED, ContactsContract.RawContacts.ACCOUNT_TYPE, ContactsContract.CommonDataKinds.Contactables.DATA, ContactsContract.CommonDataKinds.Contactables.TYPE)

    val selection = ContactsContract.Data.MIMETYPE + " in (?, ?)" + " AND " /*+ ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" + 1 + "' AND "*/ +
            ContactsContract.Data.HAS_PHONE_NUMBER + " = '" + 1 + "'"

    val selectionArgs = arrayOf(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)

    val sortOrder = ContactsContract.Contacts.SORT_KEY_ALTERNATIVE

    var uri: Uri? = null
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
        uri = ContactsContract.CommonDataKinds.Contactables.CONTENT_URI
    } else {
        uri = ContactsContract.Data.CONTENT_URI
    }
    // we could also use Uri uri = ContactsContract.Data.CONTENT_URI;
    // we could also use Uri uri = ContactsContract.Contact.CONTENT_URI;

    val cursor = context.contentResolver.query(uri!!, projection, selection, selectionArgs, sortOrder)
    val mimeTypeIdx = cursor!!.getColumnIndex(ContactsContract.Data.MIMETYPE)
    val idIdx = cursor.getColumnIndex(ContactsContract.Data.CONTACT_ID)
    val nameIdx = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)
    val dataIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Contactables.DATA)
    val photo = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Contactables.PHOTO_URI)
    val typeIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Contactables.TYPE)
    val account_type = cursor.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_TYPE)
    while (cursor.moveToNext()) {
        val contact_id = cursor.getInt(idIdx)
        val photo_uri = cursor.getString(photo)
        val contact_name = cursor.getString(nameIdx)
        val contact_acc_type = cursor.getString(account_type)
        val contact_type = cursor.getInt(typeIdx)
        val contact_data = cursor.getString(dataIdx)
        val Contact_mimeType = cursor.getString(mimeTypeIdx)
        if (Contact_mimeType == ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) {
            try {
                val number = util.parse(contact_data, countryCodeValue)
                val model = ContactModel()
                model.id = contact_id.toString()
                model.name = contact_name
                model.mobileNumber = "${number.countryCode}${number.nationalNumber}"
                //                    model.mobileNumber = contact_data
                model.photoURI = photo_uri
                list.add(model)
                //                    Log.e("Number", contact_data)
            } catch (e: Exception) {
                showlog(TAG, Log.getStackTraceString(e))
            }
        }
    }
    cursor.close()
    val endnow: Long = android.os.SystemClock.uptimeMillis()
    showlog(TAG, "TimeForContacts " + (endnow - startnow) + " ms of size " + list.size)
    return list
}

在这个google phonelibnumber中使用

实施 'com.googlecode.libphonenumber:libphonenumber:8.9.5'

Pojo 或数据类

data class ContactModel(
    var id: String = "",
    var name: String = "",
    var mobileNumber: String = "",
    var photo: Bitmap? = null,
    var photoURI: String? = "",
    var showContactInitial: Boolean = false,
    var connected: Boolean = false,
    var userid: String = "",
    var request_status: String = "",
    var hasBtwixt: Boolean = false,
    var createdby: String = "",
    var image: String = "",
    var request_id:String = ""
    )
于 2018-12-17T10:40:35.077 回答