当前位置: 首页 > news >正文

Android读取拨号记录功能

Android读取拨号记录功能

Android读取拨号记录功能
首先会检测应用是否有读取拨号记录的权限

在这里插入图片描述

MainActivity.java


public class MainActivity extends AppCompatActivity {private ListView listCalls;private List<Map<String, Object>> mapList;private static final int REQUEST_CODE = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);onShowCallLog();}public void initView() {listCalls = (ListView) super.findViewById(R.id.call_list);SimpleAdapter simpleAdapter = new SimpleAdapter(this,mapList,R.layout.call_item,new String[]{CallLog.Calls.NUMBER, CallLog.Calls.DATE},new int[]{R.id.call_mobile, R.id.call_date});listCalls.setAdapter(simpleAdapter);}private void initDate() {ContentResolver contentResolver = getContentResolver();Cursor cursor = contentResolver.query(CallLog.Calls.CONTENT_URI,new String[]{CallLog.Calls.NUMBER, CallLog.Calls.DATE},null, null, null);mapList = new ArrayList<>();SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");while (cursor.moveToNext()) {Map<String, Object> stringObjectMap = new HashMap<>();stringObjectMap.put(CallLog.Calls.NUMBER, cursor.getString(0));stringObjectMap.put(CallLog.Calls.DATE, simpleDateFormat.format(new Date(cursor.getLong(1))));mapList.add(stringObjectMap);}cursor.close();}private void onShowCallLog() {int checkCALL_LOGPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALL_LOG);if (checkCALL_LOGPermission != PackageManager.PERMISSION_GRANTED) {ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CALL_LOG}, REQUEST_CODE);} else {initDate();initView();}}@Overridepublic void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {if (requestCode == REQUEST_CODE) {if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {Toast.makeText(this, "获取权限成功", Toast.LENGTH_SHORT).show();initDate();initView();} else {Toast.makeText(this, "获取权限失败", Toast.LENGTH_SHORT).show();this.finish();}} else {super.onRequestPermissionsResult(requestCode, permissions, grantResults);}}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="50dp"android:orientation="horizontal"><TextViewandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"android:text="号码"android:textSize="26sp" /><TextViewandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"android:text="时间"android:textSize="26sp" /></LinearLayout><ListViewandroid:id="@+id/call_list"android:layout_width="match_parent"android:layout_height="wrap_content"></ListView></LinearLayout>

call_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="50dp"android:orientation="horizontal"><TextViewandroid:id="@+id/call_mobile"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"android:textSize="24sp" /><TextViewandroid:id="@+id/call_date"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"android:textSize="24sp" /></LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"><uses-permission android:name="android.permission.READ_CALL_LOG"/><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/Theme.Learn"><activityandroid:name=".MainActivity"android:exported="true"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>

相关文章:

  • 【九】Hadoop3.3.4HA高可用配置
  • Vue3 + js-echarts 实现前端大屏可视化
  • java计算机毕设课设—网上招聘系统(附源码、文章、相关截图、部署视频)
  • 扩展------零拷贝技术(Mmap,SendFile)
  • 统计语言模型——Ngram
  • SpringMVC 工作流程简述
  • 2024年华数杯数学建模竞赛——赛题浅析
  • FFmpeg实现文件夹多视频合并
  • 使用Python创建多功能文件管理器
  • AcWing食物链
  • Lua 脚本编程基础
  • 搭建nexus上传jar包,并结合jenkins运行项目
  • OpenCV||超细节的基本操作
  • Redis学习笔记——第19章 事务
  • 【算法】递归实现二分查找(优化)以及非递归实现二分查找
  • [PHP内核探索]PHP中的哈希表
  • $translatePartialLoader加载失败及解决方式
  • 《Javascript数据结构和算法》笔记-「字典和散列表」
  • 2018以太坊智能合约编程语言solidity的最佳IDEs
  • Android系统模拟器绘制实现概述
  • Angular 4.x 动态创建组件
  • Git同步原始仓库到Fork仓库中
  • iOS动画编程-View动画[ 1 ] 基础View动画
  • java8 Stream Pipelines 浅析
  • JavaScript异步流程控制的前世今生
  • JAVA并发编程--1.基础概念
  • LeetCode算法系列_0891_子序列宽度之和
  • React系列之 Redux 架构模式
  • Spark VS Hadoop:两大大数据分析系统深度解读
  • Vue2 SSR 的优化之旅
  • VuePress 静态网站生成
  • 后端_ThinkPHP5
  • 要让cordova项目适配iphoneX + ios11.4,总共要几步?三步
  • 带你开发类似Pokemon Go的AR游戏
  • 摩拜创始人胡玮炜也彻底离开了,共享单车行业还有未来吗? ...
  • 昨天1024程序员节,我故意写了个死循环~
  • ​HTTP与HTTPS:网络通信的安全卫士
  • ​sqlite3 --- SQLite 数据库 DB-API 2.0 接口模块​
  • # .NET Framework中使用命名管道进行进程间通信
  • # 利刃出鞘_Tomcat 核心原理解析(七)
  • #pragma预处理命令
  • (Mac上)使用Python进行matplotlib 画图时,中文显示不出来
  • (二十五)admin-boot项目之集成消息队列Rabbitmq
  • (非本人原创)我们工作到底是为了什么?​——HP大中华区总裁孙振耀退休感言(r4笔记第60天)...
  • (附源码)计算机毕业设计ssm基于B_S的汽车售后服务管理系统
  • (回溯) LeetCode 77. 组合
  • (佳作)两轮平衡小车(原理图、PCB、程序源码、BOM等)
  • (欧拉)openEuler系统添加网卡文件配置流程、(欧拉)openEuler系统手动配置ipv6地址流程、(欧拉)openEuler系统网络管理说明
  • (求助)用傲游上csdn博客时标签栏和网址栏一直显示袁萌 的头像
  • (十三)Java springcloud B2B2C o2o多用户商城 springcloud架构 - SSO单点登录之OAuth2.0 根据token获取用户信息(4)...
  • (四)鸿鹄云架构一服务注册中心
  • (四)模仿学习-完成后台管理页面查询
  • ***微信公众号支付+微信H5支付+微信扫码支付+小程序支付+APP微信支付解决方案总结...
  • 、写入Shellcode到注册表上线
  • .Net MVC + EF搭建学生管理系统