【问题标题】:How to speed up the receipt of contacts from android phone?如何加快从安卓手机接收联系人的速度?
【发布时间】:2018-07-19 07:33:44
【问题描述】:

我编写了从电话簿中获取联系人的代码,但我认为他的工作速度很慢。 例如,在三星 Galaxy s8 中,189 个电话号码(每个联系人可能有多个号码)的平均速度为 4582 毫秒。 5秒!我的想法很慢。

我把负责计算时间和解析检查正确性的部分代码剪掉了,对速度影响不大:

class ContactLoader(private val context: Context) {

    fun getContactList(): List<ContactEntity> {
        val result = mutableListOf<ContactEntity>()
        val contentResolver = context.contentResolver

        val cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null)

        if (cursor != null && cursor.count > 0) {
            while (cursor.moveToNext()) {
                val id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID))
                val name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))

                if (cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
                    val secondCursor = contentResolver.query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                            arrayOf(id),
                            null
                    )

                    while (secondCursor.moveToNext()) {
                        val phone = secondCursor.getString(secondCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))

                        val contactEntity = ContactEntity(
                                userId = id,
                                userName = name,
                                phone = phone,
                                phoneHash = hashUtils.fromNumberToHash(phone.replace("""\D""".toRegex(), ""))
                        )
                        result.add(contactEntity)
                    }

                    secondCursor.close()
                }
            }
        }

        cursor?.close()

        return result
    }
}

如何加快手机联系方式?

【问题讨论】:

    标签: android android-contentresolver


    【解决方案1】:
    • 尝试向光标添加投影... req 列。
    • 尝试使用后台线程执行上述功能,并使用 UI 线程更新列表。
    • 尝试使用您在第二个光标中提到的选择标准,在您的第一个光标本身中使用..避免使用嵌套语句..可以将其制成单个实体。 这可能会有所改善。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-17
      • 2020-04-15
      • 1970-01-01
      • 2011-03-11
      • 2011-03-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多