Android读取拨号记录功能

devtools/2024/10/18 14:18:38/

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>

http://www.ppmy.cn/devtools/91260.html

相关文章

【数据湖与数据仓库】数据湖与数据仓库的区别与应用

数据湖与数据仓库 数据湖与数据仓库的区别与应用 引言 随着大数据时代的到来&#xff0c;数据量呈指数级增长&#xff0c;企业需要高效的存储和管理海量数据的方法。数据湖和数据仓库是两种常见的数据存储和管理解决方案&#xff0c;它们在存储架构、数据处理方式和应用场景上…

一套基于tailwindcss的后台管理系统模板Chakra UI + React + TS

下载地址给你们&#xff1a; https://horizon-ui.com/#version

LeetCode_sql_day13(1517.查找拥有有效邮箱的用户)

描述&#xff1a;1517. 查找拥有有效邮箱的用户 - 力扣&#xff08;LeetCode&#xff09; 数据准备&#xff1a; Create table If Not Exists Users (user_id int, name varchar(30), mail varchar(50)) Truncate table Users insert into Users (user_id, name, mail) values…

uniApp跳转外链

创建一个新的页面&#xff0c;在该页面中使用web-view组件加载外部URL&#xff1a;pages/web-view/web-view <template><view><uni-list><uni-list-itemtitle"打开外部链接"click"openExternalLink"></uni-list-item></…

合并两个有序数组(LeetCode)

题目 给你两个按 非递减顺序 排列的整数数组 和 &#xff0c;另有两个整数 和 &#xff0c;分别表示 和 中的元素数目。请你 合并 到 中&#xff0c;使合并后的数组同样按 非递减顺序 排列。 注意&#xff1a;最终&#xff0c;合并后数组不应由函数返回&#xff0c;而是…

深入理解 Java SPI - 概念、原理、应用

零、前言 在当今互联网时代&#xff0c;应用程序越来越复杂&#xff0c;对于我们开发人员来说&#xff0c;如何实现高效的组件化和模块化已经成为了一个重要的问题。而 Java SPI&#xff08;Service Provider Interface&#xff09;机制&#xff0c;作为一种基于接口的服务发现…

AI时代下的程序员:深耕专业,强化软技能,共创辉煌未来

一、引言&#xff1a;AI浪潮下的编程新世界 1. 时代的脉动&#xff1a;AIGC技术重塑编程格局 在人工智能的浪潮中&#xff0c;AIGC&#xff08;AI Generated Content&#xff09;技术正以前所未有的速度改变着我们的编程世界。从ChatGPT到Midjourney&#xff0c;再到Claude&a…

离线+树状数组,ABC253 F - Operations on a Matrix

一、题目 1、题目描述 2、输入输出 2.1输入 2.2输出 3、原题链接 F - Operations on a Matrix 二、解题报告 1、思路分析 我们通过差分树状数组&#xff0c;可以轻松解决操作1 操作3我们也可以通过树状数组来获取对应列的值 关键是操作2会对操作3造成影响 所以我们先对…