Flutter通话记录管理插件call_log_excercise的使用
Flutter通话记录管理插件call_log_excercise的使用
插件介绍
call_log
是一个用于访问和查询通话记录历史的 Flutter 插件。目前该插件仅支持 Android 平台,因为 iOS 不提供访问通话记录的 API。
使用方法
要使用此插件,首先在 pubspec.yaml
文件中添加依赖项:
dependencies:
call_log: ^latest_version
然后在 Android 清单文件中添加以下权限:
<uses-permission android:name="android.permission.READ_CALL_LOG" />
该插件会自动处理权限检查和请求。
目前支持的查询参数包括:
dateFrom
dateTo
durationFrom
durationTo
name
number
字符串参数使用 LIKE
和 %
通配符进行查询。
注意事项
该插件仍然维护与 v1 嵌入的向后兼容性。这意味着在发布构建时 Gradle 会报告已弃用的 API 使用情况。当 Flutter 在未来的版本中移除 v1 嵌入时,我们将移除向后兼容性。
如果要在 Flutter 后台引擎中使用此插件(例如通过 WorkManager),请注意插件无法在后台执行时请求权限。因此,您需要手动请求 READ_CALL_LOG
和 READ_PHONE_STATE
权限。
示例代码
以下是一个完整的示例,展示如何使用 call_log
插件获取通话记录并进行查询。
获取全部通话记录
// 导入插件
import 'package:call_log/call_log.dart';
void main() async {
// 获取全部通话记录
Iterable<CallLogEntry> entries = await CallLog.get();
// 打印所有通话记录
entries.forEach((entry) {
print('ID: ${entry.id}');
print('Name: ${entry.name}');
print('Number: ${entry.number}');
print('Duration: ${entry.duration}');
print('Type: ${entry.callType}');
print('Date: ${entry.date}');
print('-----------------------------------');
});
}
查询特定条件的通话记录
// 导入插件
import 'package:call_log/call_log.dart';
import 'dart:math' as math;
void main() async {
// 获取当前时间
var now = DateTime.now();
// 设置查询的时间范围
int from = now.subtract(Duration(days: 60)).millisecondsSinceEpoch;
int to = now.subtract(Duration(days: 30)).millisecondsSinceEpoch;
// 查询符合条件的通话记录
Iterable<CallLogEntry> entries = await CallLog.query(
dateFrom: from,
dateTo: to,
durationFrom: 0,
durationTo: 60,
name: 'John Doe',
number: '901700000',
type: CallType.incoming,
);
// 打印查询结果
entries.forEach((entry) {
print('ID: ${entry.id}');
print('Name: ${entry.name}');
print('Number: ${entry.number}');
print('Duration: ${entry.duration}');
print('Type: ${entry.callType}');
print('Date: ${entry.date}');
print('-----------------------------------');
});
}
更多关于Flutter通话记录管理插件call_log_excercise的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter通话记录管理插件call_log_excercise的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
call_log_exercise
是一个用于在 Flutter 应用中管理通话记录的插件。它允许开发者读取、查询和显示设备的通话记录。以下是如何使用 call_log_exercise
插件的基本步骤:
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 call_log_exercise
插件的依赖:
dependencies:
flutter:
sdk: flutter
call_log_exercise: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
以获取依赖。
2. 导入插件
在你的 Dart 文件中导入 call_log_exercise
插件:
import 'package:call_log_exercise/call_log_exercise.dart';
3. 获取通话记录
使用 CallLogExercise
类来获取通话记录。以下是一个简单的示例,展示如何获取并显示通话记录:
import 'package:flutter/material.dart';
import 'package:call_log_exercise/call_log_exercise.dart';
class CallLogScreen extends StatefulWidget {
@override
_CallLogScreenState createState() => _CallLogScreenState();
}
class _CallLogScreenState extends State<CallLogScreen> {
List<CallLogEntry> _callLogs = [];
@override
void initState() {
super.initState();
_getCallLogs();
}
Future<void> _getCallLogs() async {
List<CallLogEntry> callLogs = await CallLogExercise.getCallLogs();
setState(() {
_callLogs = callLogs;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('通话记录'),
),
body: ListView.builder(
itemCount: _callLogs.length,
itemBuilder: (context, index) {
CallLogEntry log = _callLogs[index];
return ListTile(
title: Text(log.name ?? '未知'),
subtitle: Text(log.number),
trailing: Text(log.duration.toString()),
);
},
),
);
}
}
void main() {
runApp(MaterialApp(
home: CallLogScreen(),
));
}
4. 权限配置
在 Android 上,读取通话记录需要 READ_CALL_LOG
权限。你需要在 AndroidManifest.xml
文件中添加以下权限:
<uses-permission android:name="android.permission.READ_CALL_LOG" />
在 iOS 上,读取通话记录需要用户授权。你需要在 Info.plist
文件中添加以下键值对:
<key>NSContactsUsageDescription</key>
<string>我们需要访问您的通话记录以显示通话历史。</string>
5. 处理权限请求
在实际应用中,你可能需要在运行时请求权限。你可以使用 permission_handler
插件来处理权限请求:
import 'package:permission_handler/permission_handler.dart';
Future<void> _requestPermissions() async {
if (await Permission.contacts.request().isGranted) {
_getCallLogs();
} else {
// 处理权限被拒绝的情况
}
}