Android获取手机通话记录的方法_安卓软件开发教程-查字典教程网
Android获取手机通话记录的方法
Android获取手机通话记录的方法
发布时间:2016-12-28 来源:查字典编辑
摘要:Android如何获取手机通话记录,本文为大家揭晓。获取手机通话记录流程:1、获取ContentResolver;ContentResolv...

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

<"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调试过程中会直接崩溃,需要手动在安全中心给应用赋予读取通话记录的权限。(视个人机器情况,部分机器可能不需要手动设置)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持查字典教程网。

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新安卓软件开发学习
    热门安卓软件开发学习
    编程开发子分类