美文网首页
Android 通话记录

Android 通话记录

作者: yunhen | 来源:发表于2018-09-10 17:03 被阅读254次

查询通话记录

private static final String[] CALLLOGS_PROJECTION = new String[]{CallLog.Calls._ID,
            CallLog.Calls.CACHED_NAME, CallLog.Calls.NUMBER, CallLog.Calls.TYPE, CallLog.Calls.DATE,
            CallLog.Calls.DURATION};
/**
     * <br/>
     * 概述:获取最近10条通话记录 <br/>
     */
    public ArrayList<PersonBean> getCallLogs() {
        mCallLogBeans.clear();
        ContentResolver resolver = mContext.getContentResolver();
        // 获取手机联系人
        @SuppressLint("MissingPermission")
        Cursor phoneCursor = resolver.query(CallLog.Calls.CONTENT_URI,
                CALLLOGS_PROJECTION, null, null, "date DESC");

        if (phoneCursor != null) {
            while (phoneCursor.moveToNext()) {
                // 得到手机号码
                String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX);
                // 当手机号码为空的或者为空字段 跳过当前循环
                if (TextUtils.isEmpty(phoneNumber))
                    continue;

                // 得到联系人名称,通过数据库查询的方式有的手机有问题,通话记录不会缓存联系人姓名(中兴天机7),只能去查通讯录对应的人名
                String contactName = phoneCursor.getString(PHONES_DISPLAY_NAME_INDEX);
                if (TextUtils.isEmpty(contactName)) {
                    contactName = getPhoneTrueName(phoneNumber);
                }

                if (TextUtils.isEmpty(contactName)) continue;
                PersonBean bean = new PersonBean();
                bean._id = phoneCursor.getLong(phoneCursor.getColumnIndex(CallLog.Calls._ID));
                bean.name = contactName;
                bean.phoneNum = phoneNumber.replace(" ", "").replace("+86", "");
                bean.date = phoneCursor.getLong(phoneCursor.getColumnIndex(CallLog.Calls.DATE));
                bean.duration = phoneCursor.getString(phoneCursor.getColumnIndex(CallLog.Calls.DURATION));
                bean.type = phoneCursor.getInt(phoneCursor.getColumnIndex(CallLog.Calls.TYPE));
                bean.pinyinName = Pinyin.toPinyin(bean.name, "");
                LogUtils.instance().d(TAG, "PersonBean [" +bean.toString()+"]");
                boolean isContained = false;
                for (PersonBean bean2 : mCallLogBeans) {
                    if (bean2.phoneNum.equals(bean.phoneNum)) {
                        isContained = true;
                        break;
                    }
                }
                if (!isContained) {//如果没有这个号码则加入数组
                    mCallLogBeans.add(bean);
                    if (mCallLogBeans.size() > 8) {
                        break;
                    }
                }
            }

            phoneCursor.close();
        }

        return mCallLogBeans;
    }

秒数转时长

public static long[] secondNum2Time(String timeStr) {
        long[] longs = new long[]{0,0,0};
        if (TextUtils.isEmpty(timeStr)) return longs;
        long time = Long.parseLong(timeStr);
        long hour = time / 3600;
        long minute = time / 60 % 60;
        long second = time % 60;
        longs[0] = hour;
        longs[1] = minute;
        longs[2] = second;
        return longs;
    }

通话记录的type

public String getTypeStr() {
        if (CallLog.Calls.INCOMING_TYPE == type) {
            return "来电";
        } else if (CallLog.Calls.OUTGOING_TYPE == type) {
            return "去电";
        }else if (CallLog.Calls.MISSED_TYPE == type) {
            return "未接";
        }else if (CallLog.Calls.VOICEMAIL_TYPE == type) {
            return "语音邮件";
        }else if (CallLog.Calls.REJECTED_TYPE == type) {
            return "拒绝";
        }else if (CallLog.Calls.BLOCKED_TYPE == type) {
            return "阻止";
        } else {
            return "未知";
        }
    }

相关文章

网友评论

      本文标题:Android 通话记录

      本文链接:https://www.haomeiwen.com/subject/giosgftx.html