apps目录的contacts应用(有读取通话记录功能),是访问provider目录的provider.contacts应用(有暴露通话记录),所以要阅读Android操作系统源码-->packages/providers/ContactsProvider通话记录的(内容提供者)


阅读 com.android.providers.contacts 数据库与表

Android操作系统的文件系统目录/data/data/com.android.contacts,是访问/data/data/com.android.providers.contacts(内容提供者应用)

所以需要阅读/data/data/com.android.providers.contacts(内容提供者应用)的数据库:

首选要有一条通话记录

打开 contacts2.db

打开后:表非常多,视图非常多,等等,但只需关心,calls(通话记录表)


阅读 com.android.providers.contacts AndroidManifest.xml

android:name="CallLogProvider"  通话记录的内容提供者

android:authorities="call_log"  授权唯一标识

android:exported="true"   允许对外输出

android:readPermission="android.permission.READ_CALL_LOG" 访问者必须要配置的权限
android:writePermission="android.permission.WRITE_CALL_LOG" 访问者必须要配置的权限

    <provider android:name="CallLogProvider"
android:authorities="call_log"
android:syncable="false" android:multiprocess="false"
android:exported="true"
android:readPermission="android.permission.READ_CALL_LOG"
android:writePermission="android.permission.WRITE_CALL_LOG">
</provider>

阅读 com.android.providers.contacts CallLogProvider.java

首先要找到的就是Uri,所以搜索UriMatcher的matcher.addURI

有规律,通常情况下,matcher.addURI(授权, path, code),第二个参数 path 和数据库表名对应的 calls

private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static {
sURIMatcher.addURI(CallLog.AUTHORITY, "calls", CALLS);
sURIMatcher.addURI(CallLog.AUTHORITY, "calls/#", CALLS_ID);
sURIMatcher.addURI(CallLog.AUTHORITY, "calls/filter/*", CALLS_FILTER);
}

C应用 AndroidManifest.xml 权限的配置:

  <!--
访问操作系统短信通话记录提供者应用,需要加入的权限
android:readPermission="android.permission.READ_CALL_LOG"
android:writePermission="android.permission.WRITE_CALL_LOG"
-->
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.WRITE_CALL_LOG" />
  <!-- C应用配置拨打电话的权限 拨打电话的权限 -->
<uses-permission android:name="android.permission.CALL_PHONE" />

C应用 读取操作系统通话记录并/拨打电话/发送短信/复制号码到拨号盘 Java代码:

package liudeli.cp.client;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CallLog;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter; public class CallLogActivity extends Activity { /**
* 找寻到了Android操作系统的通话记录内容提供者的授权和Uri
* android:authorities="call_log"
* sURIMatcher.addURI(CallLog.AUTHORITY, "calls", CALLS);
*/
private final String AUTHORITY = "call_log";
private Uri callLogUri = Uri.parse("content://" + AUTHORITY + "/calls"); /**
* 系统也提供了常量的方式来获取Uir,为了学习,还是直接看源码复制出来的比较理解些
*/
/*private final String AUTHORITY = CallLog.AUTHORITY;
private Uri callLogUri = Uri.parse(CallLog.CONTENT_URI + "/calls");*/ @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_call_log); ListView listView = findViewById(R.id.listview); String[] porjecting = new String[]{"_id" ,"number", "date", "type"};
final Cursor cursor = getContentResolver().query(callLogUri,
porjecting,
null, // 不要查询条件
null, // 不要查询条件值
null); // 不排序 while (cursor.moveToNext()) {
Log.d("cccc", "" + cursor.getString(0) + " "+ cursor.getString(1) + " " + cursor.getString(2) + " "+ cursor.getString(3));
} final SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, // 上下文
R.layout.layou_calllog_item, // Item显示的布局
cursor, // 游标数据
porjecting, // 数据从哪里来(从Cursor中获取对应的字段)
new int[]{R.id.tv_id, R.id.tv_number, R.id.tv_date, R.id.tv_type} // 数据到哪里去,到Item布局里面的控件显示
);
listView.setAdapter(cursorAdapter); // 千万不能关闭 cursor.close();,否则数据展示不出来 listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
// 获取Item的值 (转换规则,传入什么类型,就转换成什么类型)
final Cursor itemCursor = (Cursor) cursorAdapter.getItem(position); /**
* 把Cursor移动到指定的行数,然后在取值 itemCursor.getString(0~9)
*/
itemCursor.moveToPosition(position); new AlertDialog.Builder(CallLogActivity.this)
.setTitle("请选择")
/*.setMessage("请选择,下面列表的功能")*/ //列表对话框不能设置这个,否则显示不出来
.setItems(new String[]{"拨打电话", "复制号码到拨号盘", "复制号码到短信编辑界面"}, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) { // 把Cursor移动到指定的行数,然后在取值 itemCursor.getString(0~9)
String number = itemCursor.getString(itemCursor.getColumnIndex("number")); /**
* 下面这三个功能,都需要隐式意图的方式去激活系统暴露的组件
* 匹配规则:只要匹配一组,就可以来
*/
switch (which) {
case 0: // 拨打电话
Intent intentCall = new Intent();
intentCall.setAction(Intent.ACTION_CALL);
intentCall.setData(Uri.parse("tel:" + number));
startActivity(intentCall);
break;
case 1: // 复制号码到拨号盘
Intent intentCopyCall = new Intent();
intentCopyCall.setAction(Intent.ACTION_DIAL);
intentCopyCall.setData(Uri.parse("tel:" + number));
startActivity(intentCopyCall);
break;
case 2: // 复制号码到短信编辑界面
Intent intentSmsEdit = new Intent();
intentSmsEdit.setAction(Intent.ACTION_VIEW);
intentSmsEdit.setData(Uri.parse("sms:" + number));
startActivity(intentSmsEdit);
break;
default:
break;
}
}
})
.show();
return false;
}
});
}
}

C应用显示的Layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> <ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" /> </RelativeLayout>

C应用显示的Layout --> ListVIew-->Item布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"> <!-- 默认比重为0 我先填充 -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="id"
android:textColor="@android:color/black"
/> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="number"
android:textColor="@android:color/black"
/> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="date"
android:textColor="@android:color/black"
android:layout_marginTop="5dp"
/> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="type"
android:textColor="@android:color/black"
android:layout_marginTop="5dp"
/> </LinearLayout> <!-- 哥们,你已经填充完来吧,剩下的空间我全部使用 -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:layout_marginLeft="20dp"
> <TextView
android:id="@+id/tv_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="id"
android:textColor="@android:color/black"
/> <TextView
android:id="@+id/tv_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="number"
android:textColor="@android:color/black"
/> <TextView
android:id="@+id/tv_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="date"
android:textColor="@android:color/black"
android:layout_marginTop="5dp"
/> <TextView
android:id="@+id/tv_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="type"
android:textColor="@android:color/black"
android:layout_marginTop="5dp"
/> </LinearLayout> </LinearLayout>

C应用效果图:


真实开发中,必须要用常量,才靠谱,万一字段变来怎么办,是吧

    /**
* 通话记录通常是有常量的
*/
Calls.Date;
CallLog.Calls._ID
....
CallLog.AUTHORITY
CallLog.Calls.NUMBER;
CallLog.CONTENT_URI
....

Android-读取操作系统通话记录并/拨打电话/发送短信/复制号码到拨号盘的更多相关文章

  1. android 入门 002 (拨打电话,发送短信)

    一.拨打电话 1.首先做好界面,代码如下: layout =>activity_main.xml 中 <LinearLayout xmlns:android="http://sc ...

  2. iOS_拨打电话/发送短信

    GitHub address : https://github.com/mancongiOS/makeACallAndSendMessage.git 功能一: 拨打电话 1.可以有提示框.提示该电话号 ...

  3. 调用 url_launcher 模块打开外部浏 览器 打开外部应用 拨打电话 发送短信

    1.Flutter url_launcher 模块    Flutter url_launcher 模块可以让我们实现打开外部浏览器.打开外部应用.发送短信.拨打电话等功能.    https://p ...

  4. Arduino+sim800C家居安防火灾报警 拨打电话 发送短信例程程序

    家居安防报警器,参考程序. 火灾报警 涉及用sim800c发短信,拨打电话通知. 接线: Sim800c 3.3V -> Arduino 3.3V Sim800c GND -> Ardui ...

  5. IOS中调用系统拨打电话发送短信

    一.调用打电话界面 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat ...

  6. html5 安卓拨打电话 发短信

    方法一: <input name=”phone_no” format=”*m” value=”13″/> <do type=”option” label=”呼出号”> < ...

  7. java攻城师之路(Android篇)--搭建开发环境、拨打电话、发送短信、布局例子

    一.搭建开发环境 1.所需资源 JDK6以上 Eclipse3.6以上 SDK17, 2.3.3 ADT17 2.安装注意事项 不要使用中文路径 如果模拟器默认路径包含中文, 可以设置android_ ...

  8. Android 打开URL中的网页和拨打电话、发送短信功能

    拨打电话需要的权限 <uses-permission android:name="android.permission.CALL_PHONE"/> 为了省事界面都写一起 ...

  9. Android中使用Intent的Action和Data属性实现点击按钮跳转到拨打电话和发送短信

    场景 点击拨打电话按钮,跳转到拨打电话页面 点击发送短信按钮,跳转到发送短信页面 注: 博客: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程 ...

随机推荐

  1. linux命令日志处理

    刘超 2018/10/8 10:32:43 zcat bi_www_activity_2018100*.log.gz |grep --color '多方电话h5_' | awk -F'|' '{pri ...

  2. [转]字符集、字符编码、XML中的中文编码

    字符集.字符编码.XML中的中文编码 作为程序员的你是不是对于ASCII .UNICODE.GB2321.UTF-7.UTF-8等等不时出现在你面前的这些有着奇怪意义的词感到很讨厌呢,是不是总觉得好象 ...

  3. cdoj914-方老师分身 I 【dijkstra】

    http://acm.uestc.edu.cn/#/problem/show/914 方老师分身 I Time Limit: 3000/1000MS (Java/Others)     Memory ...

  4. 高性能Web服务器Nginx的配置与部署研究(10)核心模块之HTTP模块Location相关指令

    一.基本语法 语法:location [= | ~ | ~* | ^~] </uri/> {...} 缺省:N/A 作用域:server 二.匹配规则 1. 四种匹配方式 = 精确匹配 ~ ...

  5. ssh的发展历程与基本原理

    一.ssh是什么 SSH(Secure Shell)是一个提供数据通信安全.远程登录.远程指令执行等功能的安全网络协议,最初提出目的是替代非安全的Telnet.rsh.rexec等远程Shell协议. ...

  6. 在 CentOS 下源码安装 Xen

    http://www.vpsee.com/2010/04/install-xen-on-centos-from-source/ 在 CentOS 源码编译安装 Xen 的过程和在 Debian 上编译 ...

  7. KVM下raw和qcow2格式磁盘文件IO测试

    1. Host OS 环境 CPU: Intel Xeon E5620 2.40GHz MEM: 16GB DISK: 500GB SATA OS: CentOS5.7 64bit 2. Guest ...

  8. C++ std::vector<bool>

    std::vector template < class T, class Alloc = allocator<T> > class vector; // generic te ...

  9. ubuntu14.04 64位安装 g2o

    参考链接:http://blog.csdn.net/jiujiu932/article/details/52248577 http://www.cnblogs.com/gaoxiang12/p/473 ...

  10. 常见的移动端Web页面问题解决方案

    1.安卓浏览器看背景图片,有些设备会模糊. 用同等比例的图片在PC机上很清楚,但是手机上很模糊,原因是什么呢? 经过研究,是devicePixelRatio作怪,因为手机分辨率太小,如果按照分辨率来显 ...