Android-9 CallLog: CACHED_NAME和CACHED_PHOTO_URI总是返回CallLog中的NULL。
创始人
2024-08-19 16:00:09
0

问题描述: 在Android 9上,使用CallLog查询通话记录时,CACHED_NAME和CACHED_PHOTO_URI总是返回NULL。

解决方法: Android 9中的CallLog API已经进行了一些更改,可能会导致CACHED_NAME和CACHED_PHOTO_URI始终返回NULL。下面是一种解决方法,使用ContentResolver查询通话记录,然后从联系人提供者中获取缓存的名称和照片URI。

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.provider.CallLog;
import android.provider.ContactsContract;

public class CallLogUtils {

    public static void retrieveCallLogDetails(ContentResolver contentResolver) {
        // 查询CallLog通话记录
        Cursor cursor = contentResolver.query(CallLog.Calls.CONTENT_URI,
                null, null, null, CallLog.Calls.DEFAULT_SORT_ORDER);

        if (cursor != null) {
            while (cursor.moveToNext()) {
                // 获取号码
                String phoneNumber = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));

                // 获取时间
                long callDate = cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DATE));

                // 获取缓存的名称
                String cachedName = getContactName(contentResolver, phoneNumber);

                // 获取缓存的照片URI
                Uri cachedPhotoUri = getContactPhotoUri(contentResolver, phoneNumber);

                // 处理通话记录数据
                // ...

            }
            cursor.close();
        }
    }

    private static String getContactName(ContentResolver contentResolver, String phoneNumber) {
        // 查询联系人
        Cursor cursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME},
                ContactsContract.CommonDataKinds.Phone.NUMBER + " = ?",
                new String[]{phoneNumber}, null);

        String contactName = null;
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            }
            cursor.close();
        }
        return contactName;
    }

    private static Uri getContactPhotoUri(ContentResolver contentResolver, String phoneNumber) {
        // 查询联系人
        Cursor cursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                new String[]{ContactsContract.CommonDataKinds.Phone.PHOTO_URI},
                ContactsContract.CommonDataKinds.Phone.NUMBER + " = ?",
                new String[]{phoneNumber}, null);

        Uri photoUri = null;
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                String photoUriString = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
                if (photoUriString != null) {
                    photoUri = Uri.parse(photoUriString);
                }
            }
            cursor.close();
        }
        return photoUri;
    }
}

上述代码中,我们首先使用ContentResolver查询CallLog通话记录,然后使用查询到的电话号码去查询联系人提供者,从而获取到缓存的名称和照片URI。这样可以解决Android 9上CallLog中CACHED_NAME和CACHED_PHOTO_URI始终返回NULL的问题。

相关内容

热门资讯

透视攻略(WEPOKER)we... 透视攻略(WEPOKER)wepoker免费脚本咨询(透视)果然存在有挂(解密教程)1、玩家可以在w...
透视最新(wpk透视插件)其实... 透视最新(wpk透视插件)其实是真的有挂(透视)刷入池率脚本(解密教程)1、每一步都需要思考,不同水...
透视辅助(wepoker)we... 透视辅助(wepoker)we-poker正规吗(透视)切实真的有挂(2025新版总结)1、we-p...
透视真的(wpk模拟器是什么)... 透视真的(wpk模拟器是什么)确实有挂(透视)辅助(存在挂教程)1、进入到wpk模拟器是什么黑科技之...
透视智能ai(wpk俱乐部辅助... 透视智能ai(wpk俱乐部辅助器)确实是真的有挂(透视)辅助软件(分享教程)1、进入到wpk俱乐部辅...
透视透视(WePoKer)we... 透视透视(WePoKer)wepoker有没有挂(透视)总是存在有挂(我来教教你)1、玩家可以在we...
透视科技(wpk私人辅助)好像... 透视科技(wpk私人辅助)好像是有挂(透视)模拟器(分享教程);1、点击下载安装,wpk私人辅助插件...
透视ai(WEPOKER)破解... 透视ai(WEPOKER)破解辅助插件wepoker(透视)一直有挂(AI教程)一、破解辅助插件we...
透视辅助(购买的wpk辅助在哪... 透视辅助(购买的wpk辅助在哪里下载)果然有挂(透视)模拟器(wepoke教程);1、超多福利:超高...
辅助透视(WePoKer)we... 辅助透视(WePoKer)wepoker有插件吗(透视)好像真的有挂(揭秘教程)1、实时wepoke...