Android:如何搜索特定号码的通话记录
创始人
2024-10-13 16:31:16
0

要搜索特定号码的通话记录,可以使用以下代码示例:

public ArrayList searchCallLogs(Context context, String phoneNumber) {
    ArrayList callLogs = new ArrayList<>();
    
    // 获取通话记录的URI
    Uri callUri = CallLog.Calls.CONTENT_URI;
    
    // 定义要查询的字段
    String[] projection = new String[]{
            CallLog.Calls.NUMBER,
            CallLog.Calls.DATE,
            CallLog.Calls.DURATION,
            CallLog.Calls.TYPE
    };
    
    // 设置查询条件
    String selection = CallLog.Calls.NUMBER + " = ?";
    String[] selectionArgs = new String[]{ phoneNumber };
    
    // 查询通话记录
    Cursor cursor = context.getContentResolver().query(
            callUri,
            projection,
            selection,
            selectionArgs,
            CallLog.Calls.DATE + " DESC"
    );
    
    // 遍历查询结果
    if (cursor != null && cursor.moveToFirst()) {
        do {
            String number = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
            String date = cursor.getString(cursor.getColumnIndex(CallLog.Calls.DATE));
            String duration = cursor.getString(cursor.getColumnIndex(CallLog.Calls.DURATION));
            String type = cursor.getString(cursor.getColumnIndex(CallLog.Calls.TYPE));
            
            // 将通话记录添加到列表中
            callLogs.add("Number: " + number + ", Date: " + date + ", Duration: " + duration + ", Type: " + type);
        } while (cursor.moveToNext());
        
        cursor.close();
    }
    
    return callLogs;
}

要使用上述代码示例,可以在Android应用的活动或片段中调用searchCallLogs方法,并传入要搜索的电话号码。该方法将返回一个包含匹配的通话记录的字符串列表。请确保在使用此代码之前已经获取了通话记录的读取权限。

示例用法:

ArrayList callLogs = searchCallLogs(getApplicationContext(), "9876543210");

for (String callLog : callLogs) {
    Log.d("Call Log", callLog);
}

这将打印出与电话号码"9876543210"相关的通话记录。

相关内容

热门资讯

安装apache-beam==... 出现此错误可能是因为用户的Python版本太低,而apache-beam==2.34.0需要更高的P...
避免在粘贴双引号时向VS 20... 在粘贴双引号时向VS 2022添加反斜杠的问题通常是由于编辑器的自动转义功能引起的。为了避免这个问题...
Android Recycle... 要在Android RecyclerView中实现滑动卡片效果,可以按照以下步骤进行操作:首先,在项...
omi系统和安卓系统哪个好,揭... OMI系统和安卓系统哪个好?这个问题就像是在问“苹果和橘子哪个更甜”,每个人都有自己的答案。今天,我...
原生ios和安卓系统,原生对比... 亲爱的读者们,你是否曾好奇过,为什么你的iPhone和安卓手机在操作体验上有着天壤之别?今天,就让我...
Android - 无法确定任... 这个错误通常发生在Android项目中,表示编译Debug版本的Java代码时出现了依赖关系问题。下...
Android - NDK 预... 在Android NDK的构建过程中,LOCAL_SRC_FILES只能包含一个项目。如果需要在ND...
Akka生成Actor问题 在Akka框架中,可以使用ActorSystem对象生成Actor。但是,当我们在Actor类中尝试...
Agora-RTC-React... 出现这个错误原因是因为在 React 组件中使用,import AgoraRTC from “ago...
Alertmanager在pr... 首先,在Prometheus配置文件中,确保Alertmanager URL已正确配置。例如:ale...