当前位置: 移动技术网 > 移动技术>移动开发>Android > Android获取手机通话记录的方法

Android获取手机通话记录的方法

2019年07月24日  | 移动技术网移动技术  | 我要评论

android如何获取手机通话记录,本文为大家揭晓。

获取手机通话记录流程:

1、 获取contentresolver;

contentresolver resolver = getcontentresolver();

2、resolver.query(*);

需要传入通话记录的uri:calllog.calls.content_uri

3、对查询得到的cursor进行数据获取.

主要代码如下:

mainactivity.java

package com.noonecode.contentresolvercalllogdemo;

import java.text.simpledateformat;
import java.util.arraylist;
import java.util.date;
import java.util.hashmap;
import java.util.list;
import java.util.map;

import android.app.activity;
import android.content.contentresolver;
import android.database.cursor;
import android.os.bundle;
import android.provider.calllog;
import android.widget.listview;
import android.widget.simpleadapter;

public class mainactivity extends activity {
  private listview mlvshow;
  private list<map<string, string>> datalist;
  private simpleadapter adapter;

  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
    mlvshow = (listview) findviewbyid(r.id.lv_show);
    datalist = getdatalist();
    adapter = new simpleadapter(this, datalist, r.layout.simple_calllog_item//
        , new string[] { "name", "number", "date", "duration", "type" }//
        , new int[] { r.id.tv_name, r.id.tv_number, r.id.tv_date, r.id.tv_duration, r.id.tv_type });
    mlvshow.setadapter(adapter);
  }

  /**
   * 读取数据
   * 
   * @return 读取到的数据
   */
  private list<map<string, string>> getdatalist() {
    // 1.获得contentresolver
    contentresolver resolver = getcontentresolver();
    // 2.利用contentresolver的query方法查询通话记录数据库
    /**
     * @param uri 需要查询的uri,(这个uri是contentprovider提供的)
     * @param projection 需要查询的字段
     * @param selection sql语句where之后的语句
     * @param selectionargs ?占位符代表的数据
     * @param sortorder 排序方式
     * 
     */
    cursor cursor = resolver.query(calllog.calls.content_uri, // 查询通话记录的uri
        new string[] { calllog.calls.cached_name// 通话记录的联系人
            , calllog.calls.number// 通话记录的电话号码
            , calllog.calls.date// 通话记录的日期
            , calllog.calls.duration// 通话时长
            , calllog.calls.type }// 通话类型
        , null, null, calllog.calls.default_sort_order// 按照时间逆序排列,最近打的最先显示
    );
    // 3.通过cursor获得数据
    list<map<string, string>> list = new arraylist<map<string, string>>();
    while (cursor.movetonext()) {
      string name = cursor.getstring(cursor.getcolumnindex(calllog.calls.cached_name));
      string number = cursor.getstring(cursor.getcolumnindex(calllog.calls.number));
      long datelong = cursor.getlong(cursor.getcolumnindex(calllog.calls.date));
      string date = new simpledateformat("yyyy-mm-dd hh-mm-ss").format(new date(datelong));
      int duration = cursor.getint(cursor.getcolumnindex(calllog.calls.duration));
      int type = cursor.getint(cursor.getcolumnindex(calllog.calls.type));
      string typestring = "";
      switch (type) {
      case calllog.calls.incoming_type:
        typestring = "打入";
        break;
      case calllog.calls.outgoing_type:
        typestring = "打出";
        break;
      case calllog.calls.missed_type:
        typestring = "未接";
        break;
      default:
        break;
      }
      map<string, string> map = new hashmap<string, string>();
      map.put("name", (name == null) ? "未备注联系人" : name);
      map.put("number", number);
      map.put("date", date);
      map.put("duration", (duration / 60) + "分钟");
      map.put("type", typestring);
      list.add(map);
    }
    return list;
  }
}

主布局activity_main.xml

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.noonecode.contentresolvercalllogdemo.mainactivity" >

  <listview
    android:id="@+id/lv_show"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</relativelayout>

simple_calllog_item.xml

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:padding="10dp" >

  <textview
    android:id="@+id/tv_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="name"
    android:textsize="20sp" />

  <linearlayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <textview
      android:id="@+id/tv_number"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_margin="4dp"
      android:text="number"
      />

    <textview
      android:id="@+id/tv_date"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_margin="4dp"
      android:text="date"
      />

    <textview
      android:id="@+id/tv_duration"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_margin="4dp"
      android:text="duration"
      />

    <textview
      android:id="@+id/tv_type"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_margin="4dp"
      android:text="type"
      />
  </linearlayout>

</linearlayout>

读取通话记录的权限:

<uses-permission android:name="android.permission.read_call_log" />

最终效果图:

注意:

夜神模拟器貌似无打电话的功能,不要使用夜神测试本例
版主使用的是小米4真机测试,usb调试过程中会直接崩溃,需要手动在安全中心给应用赋予读取通话记录的权限。(视个人机器情况,部分机器可能不需要手动设置)

源码下载:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网