Flutter通话记录管理插件call_log_new的使用
Flutter通话记录管理插件call_log_new的使用
特性
call_log_new 插件提供了对 Android 设备上通话记录的访问功能。您可以获取诸如电话号码、通话类型、通话日期和通话时长等详细信息。
开始使用
安装
要将 call_log_new 插件添加到您的 Flutter 项目中,请将其作为依赖项添加到 pubspec.yaml
文件中:
dependencies:
call_log_new: ^1.0.4 # 替换为最新版本
运行 flutter pub get
来安装插件。
使用
通过以下方式获取通话记录:
import 'package:call_log_new/call_log_new.dart';
Iterable<CallLogResponse> callLogs = [];
void fetchCallLogs() async {
try {
final logs = await CallLog.fetchCallLogs();
print(logs);
} catch (e) {
print("Error fetching call logs: $e");
}
}
处理权限
该插件需要访问通话记录的权限。确保在应用中处理必要的权限:
import 'package:permission_handler/permission_handler.dart';
void requestPermissions() async {
if (await Permission.phone.request().isGranted &&
await Permission.contacts.request().isGranted) {
fetchCallLogs();
} else {
print("Permissions denied");
}
}
示例代码
以下是一个完整的示例代码,展示了如何在 Flutter 应用中使用 call_log_new 插件来获取并显示通话记录。
example/lib/main.dart
import 'package:call_log_new/call_log_new.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Call Log Example',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: CallLogScreen(),
);
}
}
class CallLogScreen extends StatefulWidget {
const CallLogScreen({super.key});
[@override](/user/override)
_CallLogScreenState createState() => _CallLogScreenState();
}
class _CallLogScreenState extends State<CallLogScreen> {
Iterable<CallLogResponse> callLogs = [];
[@override](/user/override)
void initState() {
super.initState();
fetchCallLogs();
}
Future<void> fetchCallLogs() async {
// 请求权限
final status = await Permission.phone.request();
if (status.isGranted) {
try {
final logs = await CallLog.fetchCallLogs();
setState(() {
callLogs = logs;
});
} catch (e) {
print("Error fetching call logs: $e");
}
} else {
print("Permission denied");
}
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('通话记录')),
body: callLogs.isEmpty
? const Center(child: Text('没有通话记录'))
: ListView.builder(
itemCount: callLogs.length,
itemBuilder: (context, index) {
final log = callLogs.elementAt(index);
return ListTile(
title: Text(log.name ?? log.number ?? '未知'),
subtitle: Text(
"类型: ${log.callType}, SIM卡: ${log.simDisplayName}",
),
);
},
),
);
}
}
更多关于Flutter通话记录管理插件call_log_new的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter通话记录管理插件call_log_new的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,下面是一个关于如何在Flutter项目中使用call_log_new
插件来管理通话记录的基本示例代码。这个插件允许你读取和写入设备的通话记录。请确保你已经将call_log_new
插件添加到你的pubspec.yaml
文件中,并且已经正确配置了Android和iOS的权限。
pubspec.yaml
首先,确保你的pubspec.yaml
文件中包含call_log_new
插件的依赖项:
dependencies:
flutter:
sdk: flutter
call_log_new: ^x.y.z # 请替换为最新版本号
读取通话记录
下面是一个简单的例子,演示如何读取通话记录:
import 'package:flutter/material.dart';
import 'package:call_log_new/call_log_new.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
List<CallLog> callLogs = [];
@override
void initState() {
super.initState();
_getCallLogs();
}
Future<void> _getCallLogs() async {
try {
callLogs = await CallLogNew.getCallLogs();
setState(() {});
} catch (e) {
print(e);
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('通话记录'),
),
body: ListView.builder(
itemCount: callLogs.length,
itemBuilder: (context, index) {
CallLog callLog = callLogs[index];
return ListTile(
title: Text('号码: ${callLog.phoneNumber}'),
subtitle: Text('类型: ${callLog.type == CallType.incoming ? '来电' : '去电'}'),
trailing: Text('时长: ${callLog.duration} 秒'),
);
},
),
),
);
}
}
写入通话记录
下面是一个简单的例子,演示如何写入通话记录(注意:在大多数设备上,写入通话记录可能需要设备的root权限或者特定的系统权限,这在普通应用中可能不被允许):
Future<void> _addCallLog() async {
try {
CallLog newCallLog = CallLog(
phoneNumber: '1234567890',
type: CallType.outgoing, // 设置为来电或去电
date: DateTime.now(),
duration: 60, // 通话时长,单位为秒
);
bool success = await CallLogNew.addCallLog(newCallLog);
if (success) {
print('通话记录添加成功');
// 重新获取通话记录以更新UI
_getCallLogs();
} else {
print('通话记录添加失败');
}
} catch (e) {
print(e);
}
}
你可以将_addCallLog
方法添加到你的_MyAppState
类中,并在某个按钮点击事件中调用它,例如:
floatingActionButton: FloatingActionButton(
onPressed: _addCallLog,
tooltip: '添加通话记录',
child: Icon(Icons.add),
),
权限请求
在Android和iOS上,你需要请求访问通话记录的权限。对于Android,你可以在AndroidManifest.xml
中添加以下权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.WRITE_CALL_LOG" />
对于iOS,你需要在Info.plist
中添加相应的权限描述,并在代码中请求这些权限。
请注意,由于隐私和安全性的考虑,现代操作系统对访问通话记录等敏感数据的权限控制非常严格,因此在实际应用中,你可能需要处理更多的权限请求和用户同意逻辑。
以上代码提供了一个基本框架,用于在Flutter中使用call_log_new
插件来管理通话记录。根据你的具体需求,你可能需要进一步调整和完善这些代码。