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的问题。

相关内容

热门资讯

总算明白!云龙干瞪眼雀牌辅助功... 总算明白!云龙干瞪眼雀牌辅助功能app,云龙干瞪眼雀牌好像是有辅助app(有辅助挂头条)云龙干瞪眼雀...
交流学习经验!乐天棋牌辅助功能... 交流学习经验!乐天棋牌辅助功能插件,乐天棋牌本来是真的有辅助插件(有辅助挂辅教程)1.乐天棋牌 选牌...
技术分享!红拐弯开挂功能教程,... 技术分享!红拐弯开挂功能教程,红拐弯总是真的有开挂教程(有辅助挂app)在进入红拐弯软件靠谱后,参与...
教程攻略!奇趣互娱辅助功能方法... 教程攻略!奇趣互娱辅助功能方法,奇趣互娱切实真的有辅助方法(详细教程辅助)一、奇趣互娱游戏安装教程牌...
今日头条!福麻圈开挂功能神器,... 今日头条!福麻圈开挂功能神器,福麻圈其实是真的有开挂神器(有辅助挂透视)进入游戏-大厅左侧-新手福利...
推荐一款!大富豪开挂功能方法,... 推荐一款!大富豪开挂功能方法,大富豪总是是真的有开挂方法(有辅助挂实锤)1、全新机制【大富豪ai辅助...
带你了解!星火燎原开挂功能ap... 带你了解!星火燎原开挂功能app,星火燎原本来真的有开挂app(有辅助挂辅助)1、星火燎原辅助器安装...
新手必备!微信十三水辅助功能工... 新手必备!微信十三水辅助功能工具,微信十三水果然存在有辅助工具(有辅助挂细节)1)微信十三水辅助插件...
分享实测!澳门风云辅助器功能攻... 分享实测!澳门风云辅助器功能攻略,澳门风云一直存在有辅助器攻略(有辅助挂方略)澳门风云破解侠是真的助...
避坑细节!家乡大贰辅助器功能插... 避坑细节!家乡大贰辅助器功能插件,家乡大贰总是是真的有辅助器插件(有辅助挂辅助)1)家乡大贰有没有挂...