Flutter通话监听插件call_watcher的使用
Flutter通话监听插件call_watcher的使用
call_watcher
是一个用于监听通话状态并维护通话记录的 Flutter 插件,在 iOS 上使用 CallKit
。
使用
首先,确保在项目中引入 call_watcher
包:
dependencies:
call_watcher: ^版本号
然后,你可以使用以下方法来监听通话状态和获取通话记录:
初始化通话
import 'package:call_watcher/call_watcher.dart';
void initiateCall() async {
try {
await CallWatcher.initiateCall("1234567890");
} catch (e) {
print(e);
}
}
获取通话记录
void getCallLogs() {
CallWatcher.getCallLogs().then((logs) {
print(logs);
});
}
结束当前通话
void endCurrentCall() async {
try {
await CallWatcher.endCurrentCall();
} catch (e) {
print(e);
}
}
获取最后拨打的号码
void getLastCalledNumber() {
CallWatcher.getLastCalledNumber().then((number) {
print(number);
});
}
查询通话记录
final query = LogQuery(
name: 'John Doe',
number: '1234567890',
isOutgoing: true,
dateFrom: DateTime.now().subtract(Duration(days: 7)),
dateTo: DateTime.now(),
durationFrom: 0,
durationTo: 100,
);
final logs = await CallWatcher.getQueryCallLogs(query);
平台特定设置
iOS
在 ios/Runner/Info.plist
中添加以下权限:
<key>NSUserActivityTypes</key>
<array>
<string>INStartAudioCallIntent</string>
</array>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>tel</string>
</array>
Android
在 AndroidManifest.xml
中添加以下权限和服务:
<service android:name=".CallControlService"
android:permission="android.permission.BIND_INCALL_SERVICE"
android:exported="true">
<intent-filter>
<action android:name="android.telecom.InCallService"/>
</intent-filter>
</service>
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<!-- 如果你想清除通话记录(可选) -->
<uses-permission android:name="android.permission.WRITE_CALL_LOG" />
示例代码
以下是完整的示例代码,展示了如何使用 call_watcher
插件进行通话监听和管理:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:call_watcher/call_watcher.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
[@override](/user/override)
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
String _lastCalledNumber = 'Unknown';
final List<CallLogEntry> _callLogs = [];
final TextEditingController _numberController = TextEditingController();
[@override](/user/override)
void initState() {
super.initState();
_getLastCalledNumber();
WidgetsBinding.instance.addObserver(this);
}
// 这个方法将在应用生命周期变化时触发。
[@override](/user/override)
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
switch (state) {
case AppLifecycleState.resumed:
print("App is in the foreground (resumed)");
_getCallLogs();
break;
case AppLifecycleState.inactive:
print("App is inactive (e.g., incoming call or switching apps)");
// 应用处于非活动状态,暂停任务。
break;
default:
break;
}
}
[@override](/user/override)
void dispose() {
// 移除观察者。
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
Future<void> _initiateCall(String number) async {
int? result;
try {
result = await CallWatcher.initiateCall(number);
} catch (e) {
print(e);
}
if (!mounted) return;
print(result);
setState(() {
_lastCalledNumber = number;
});
}
Future<void> _getLastCalledNumber() async {
String? lastCalledNumber;
try {
lastCalledNumber = await CallWatcher.getLastCalledNumber();
} on PlatformException {
lastCalledNumber = 'Failed to get last called number.';
}
if (!mounted) return;
setState(() {
_lastCalledNumber = lastCalledNumber ?? _lastCalledNumber;
});
}
Future<void> _getCallLogs() async {
List<CallLogEntry>? callLogs;
try {
callLogs = await CallWatcher.getCallLogs(limit: 10);
if (callLogs != null) {
print(callLogs.length);
}
} on PlatformException {
callLogs = [];
}
if (!mounted) return;
setState(() {
_callLogs.clear();
_callLogs.addAll(callLogs ?? []);
});
}
double get bottomInset => MediaQuery.of(context).viewInsets.bottom;
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
floatingActionButton: bottomInset == 0
? null
: FloatingActionButton(
onPressed: () {
FocusScope.of(context).unfocus();
},
child: const Icon(Icons.keyboard_hide),
),
body: Center(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Last called number: $_lastCalledNumber\n'),
// 输入框以输入要拨打的号码并发起呼叫
Padding(
padding: const EdgeInsets.only(bottom: 15.0),
child: Row(
children: [
Expanded(
child: TextField(
decoration: const InputDecoration(
hintText: 'Enter the number to call',
),
controller: _numberController,
keyboardType: TextInputType.phone,
onSubmitted: _initateCall,
),
),
IconButton(
onPressed: () {
_initateCall(_numberController.text);
},
icon: const Icon(CupertinoIcons.phone),
),
],
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: _getLastCalledNumber,
child: const Text('Get Last Number'),
),
const SizedBox(
width: 10,
),
ElevatedButton(
onPressed: _getCallLogs,
child: const Text('Call Logs'),
),
],
),
Expanded(
child: ListView.builder(
itemCount: _callLogs.length,
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 10),
itemBuilder: (context, index) {
return Row(
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(_callLogs[index].number),
const SizedBox(width: 10),
Text(
'Date: ${_callLogs[index].date?.formatDate}'),
],
),
),
Column(
children: [
Icon(
_callLogs[index].isOutgoing
? CupertinoIcons.phone_arrow_up_right
: CupertinoIcons.phone_arrow_down_left,
color: _callLogs[index].isOutgoing
? Colors.green
: Colors.red,
),
Text('${_callLogs[index].duration?.inSeconds} sec'),
],
),
],
);
},
),
),
],
),
),
),
),
);
}
}
extension on DateTime {
String get formatDate {
final localDate = toLocal();
return '${localDate.hour}:${localDate.minute} ${localDate.day}/${localDate.month}/${localDate.year}';
}
}
更多关于Flutter通话监听插件call_watcher的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter通话监听插件call_watcher的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter项目中使用call_watcher
插件来监听通话事件的示例代码。call_watcher
插件主要用于在Android平台上监听来电、去电和未接电话事件。
前提条件
- 确保你已经安装了Flutter和Dart开发环境。
- 在你的
pubspec.yaml
文件中添加call_watcher
依赖:
dependencies:
flutter:
sdk: flutter
call_watcher: ^x.y.z # 请将x.y.z替换为最新版本号
1. 配置Android权限
首先,你需要在android/app/src/main/AndroidManifest.xml
文件中添加必要的权限:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yourapp">
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<application
... >
...
</application>
</manifest>
2. 请求权限
在Flutter代码中,你需要请求这些权限。可以在MainActivity.kt
(或MainActivity.java
)中添加权限请求逻辑,或者使用Flutter的permission_handler
插件来请求。
3. 使用call_watcher
插件
以下是一个完整的Flutter示例代码,展示如何使用call_watcher
插件来监听通话事件:
import 'package:flutter/material.dart';
import 'package:call_watcher/call_watcher.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String? callEventType;
String? callNumber;
String? callDuration;
DateTime? callTimestamp;
@override
void initState() {
super.initState();
_setupCallWatcher();
}
void _setupCallWatcher() {
CallWatcher.listen((CallEvent event) {
setState(() {
callEventType = event.type.toString();
callNumber = event.number;
callDuration = event.duration?.inSeconds?.toString() ?? 'N/A';
callTimestamp = event.timestamp;
});
// 打印通话事件信息到控制台
print('Call Event: ${event.toJson()}');
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Call Watcher Demo'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Call Event Type:', style: TextStyle(fontSize: 18)),
Text(callEventType ?? 'N/A', style: TextStyle(fontSize: 16)),
SizedBox(height: 16),
Text('Call Number:', style: TextStyle(fontSize: 18)),
Text(callNumber ?? 'N/A', style: TextStyle(fontSize: 16)),
SizedBox(height: 16),
Text('Call Duration (seconds):', style: TextStyle(fontSize: 18)),
Text(callDuration ?? 'N/A', style: TextStyle(fontSize: 16)),
SizedBox(height: 16),
Text('Call Timestamp:', style: TextStyle(fontSize: 18)),
Text(callTimestamp?.toLocal().toString() ?? 'N/A', style: TextStyle(fontSize: 16)),
],
),
),
),
);
}
}
4. 运行应用
确保你的Flutter开发环境配置正确,然后在终端中运行以下命令来构建和运行应用:
flutter run
注意事项
call_watcher
插件目前仅支持Android平台。- 请确保你的应用符合Google Play商店的政策,特别是在处理用户隐私和数据时。
- 权限请求和处理可能需要根据你的应用需求进行调整。
希望这个示例代码能帮助你更好地理解和使用call_watcher
插件。