安卓工具类分享-查询通话记录

    xiaoxiao2025-11-13  1

    查询用户通话记录的工具类分享给大家

    package com.warmdoctor.service.util; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.database.Cursor; import android.provider.CallLog; import android.provider.CallLog.Calls; /** * Copyright 2016 CoderDream's Eclipse * * All right reserved. * * Created on 2016年8月15日 下午5:02:45 * * Update on 2016年8月15日 下午5:02:45 * * @author xiaoming * * @mail sgyingyin@sina.com * * @tags An overview of this file: 查询用户通话记录 * */ public class CallUtil { // 请在Manifest文件中声明权限 <uses-permission android:name="android.permission.READ_CALL_LOG" /> private static CallUtil callUtil; private Context mContext; private CallUtil(Context context) { mContext = context; } public synchronized static CallUtil getInstance(Context context) { if (callUtil == null) callUtil = new CallUtil(context); return callUtil; } /** * @author xiaoming 2016年8月15日 * @describe 获取所有的通话记录 * @return * @returnType List<CallsLog> */ public List<CallsLog> getCallLog() { if(mContext == null) return null; List<CallsLog> logs = new ArrayList<CallsLog>(); Cursor cursor = mContext.getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, null); if (cursor.moveToFirst()) { do { // 号码 String number = cursor.getString(cursor.getColumnIndex(Calls.NUMBER)); // 呼叫类型 int type = Integer.parseInt(cursor.getString(cursor.getColumnIndex(Calls.TYPE))); // 呼叫时间 long date = Long.parseLong(cursor.getString(cursor.getColumnIndexOrThrow(Calls.DATE))); // 联系人 String cached_name = cursor.getString(cursor.getColumnIndexOrThrow(Calls.CACHED_NAME)); // 通话时间,单位:s long duration = Long.parseLong(cursor.getString(cursor.getColumnIndexOrThrow(Calls.DURATION))); CallsLog calls = new CallsLog(number, type, date, cached_name, duration); logs.add(calls); } while (cursor.moveToNext()); } return logs; } /** * @author xiaoming 2016年8月15日 * @describe 查询某人的通话记录 * @param numberStr 电话号码 * @return * @returnType List<CallsLog> */ public List<CallsLog> getCallLogOfNumber(String numberStr){ if(mContext == null) return null; List<CallsLog> logs = new ArrayList<CallsLog>(); String selection = "number=" + numberStr; Cursor cursor = mContext.getContentResolver().query(CallLog.Calls.CONTENT_URI, null, selection, null, null); if (cursor.moveToFirst()) { do { // 号码 String number = cursor.getString(cursor.getColumnIndex(Calls.NUMBER)); // 呼叫类型 int type = Integer.parseInt(cursor.getString(cursor.getColumnIndex(Calls.TYPE))); // 呼叫时间 long date = Long.parseLong(cursor.getString(cursor.getColumnIndexOrThrow(Calls.DATE))); // 联系人 String cached_name = cursor.getString(cursor.getColumnIndexOrThrow(Calls.CACHED_NAME)); // 通话时间,单位:s long duration = Long.parseLong(cursor.getString(cursor.getColumnIndexOrThrow(Calls.DURATION))); CallsLog calls = new CallsLog(number, type, date, cached_name, duration); logs.add(calls); } while (cursor.moveToNext()); } return logs; } public class CallsLog { /** * 号码 */ private String number; /** * 呼叫类型 Calls.INCOMING_TYPE呼入 Calls.OUTGOING_TYPE呼出 Calls.MISSED_TYPE未接 * Calls.MISSED_TYPE语音信箱 */ private int type; /** * 通话日期 */ private long date; /** * 联系人 */ private String cached_name; /** * 通话时长 */ private long duration; public CallsLog(String number, int type, long date, String cached_name, long duration) { super(); this.number = number; this.type = type; this.date = date; this.cached_name = cached_name; this.duration = duration; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public int getType() { return type; } public void setType(int type) { this.type = type; } public long getDate() { return date; } public void setDate(long date) { this.date = date; } public String getCached_name() { return cached_name; } public void setCached_name(String cached_name) { this.cached_name = cached_name; } public long getDuration() { return duration; } public void setDuration(long duration) { this.duration = duration; } @Override public String toString() { return "CallsLog [number=" + number + ", type=" + type + ", date=" + date + ", cached_name=" + cached_name + ", duration=" + duration + "]"; } } }
    转载请注明原文地址: https://ju.6miu.com/read-1304158.html
    最新回复(0)