大佬教程收集整理的这篇文章主要介绍了Android获取手机通话记录的方法,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
Android如何获取手机通话记录,本文为大家揭晓。
获取手机通话记录流程:
1、 获取ContentResolver;
ContentResolver resolver = getContentResolver();
2、resolver.query(*);
需要传入通话记录的URI:CallLog.Calls.CONTENT_URI
主要代码如下:
@H_464_0@mainActivity.javapackage 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,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://scheR_172_11845@as.android.com/apk/res/android" xmlns:tools="http://scheR_172_11845@as.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://scheR_172_11845@as.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调试过程中会直接崩溃,需要手动在安全中心给应用赋予读取通话记录的权限。(视个人机器情况,部分机器可能不需要手动设置)
源码下载:http://xiazai.code.net/201610/yuanma/androidContentDemo(code.net).rar
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
以上是大佬教程为你收集整理的Android获取手机通话记录的方法全部内容,希望文章能够帮你解决Android获取手机通话记录的方法所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。