CallLog.Calls.CACHED_NAME is always returning null for some saved contacts

↘锁芯ラ 提交于 2019-12-04 13:31:27

I also came across this strange behavior and figured out that sometimes you can't get name of the contact immediately even though you can get everything else from the CallLog.Calls. What I noticed is that after approximately one hour or so since that call you can fetch name along with all other CallLog.Calls data. Very strange. If you need immediate refresh after call you can get name for the number from ContactsContract like this:

 fun getNameForNumber(context: Context, number: String): String? {

        var res: String? = null
        try {
            val resolver = context.contentResolver
            val uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number))
            val c = resolver.query(uri, arrayOf(ContactsContract.PhoneLookup.DISPLAY_NAME), null, null, null)

            if (c != null) {
                if (c.moveToFirst()) {
                    res = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME))
                }
                c.close()
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }

        return res
    }

Strangely, after you fetch one name from the ContactsContract fetching name in the way you did it, through CallLog.Calls.CACHED_NAME, misteriously works.

This is also answer for @PeterB and @shyam002

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!