搜索插件、代码仓与 APP 分类

插件详情v0.0.4

phone_state_background

一个用于处理电话通话状态并在后台执行 Dart 回调的 Flutter 插件。

Android
近 30 天下载
208
喜欢数
26
Pub 分数
160

安装插件

flutter pub add phone_state_background

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

phone_state_background 包是一个专为 Android 设计的 Flutter 插件,用于监控电话通话状态,并在应用处于后台时仍能触发 Dart 回调。它使开发者能够在来电、去电或未接来电时执行自定义逻辑,而无需应用处于活动运行状态。这对于通话记录、分析或在通话事件期间与其他服务集成特别有用。该插件利用 Android 的广播接收器,在应用生命周期变化时保持功能持续可用。

适合场景

  • 呼叫日志
  • 实时通话监控
  • 后台调用事件处理
  • 与电话服务的集成
  • 对呼叫状态变化的自动响应

主要特点

  • 后台执行支持
  • Dart 回调,用于调用状态更改
  • 仅限 Android 的平台兼容性
  • 简单的 API 集成
  • 基于广播接收器的架构

适合对象

  • 开发电话应用程序的开发者
  • 需要后台通话追踪的应用
  • 调用分析工具
  • 面向服务的移动应用

使用建议

  • 仅限 Android 平台
  • 需要正确的 Android 清单权限
  • 可能影响电池续航
  • 较新 Android 版本上的后台执行限制
  • 无 iOS 支持

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

phone_state_background

A flutter plugin to handle Phone Call State and execute a Dart callback in background.

Warning

This plugin only supported in android platform.


IOS Implementation

Corrently IOS is not supported. Unfortunately I'm not familiar with IOS development neither with Switf/ObjC languages, so if you wish to help any PR will be welcome.


Android

After importing this plugin to your project as usual, add the following to your AndroidManifest.xml within the <manifest></manifest> tags:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />

Also, apparently it is necessary to register the broadcast receiver manually, otherwise an error will be throw saying that our receiver does not exist inside the app. Within the <application></application> tags:,

<receiver android:name="me.sodipto.phone_state_background.PhoneStateBackgroundServiceReceiver"
    android:exported="false">
    <intent-filter>
        <action android:name="android.intent.action.PHONE_STATE" />
    </intent-filter>
</receiver>

Getting started

All you need to start using our package after installing it, is defining a callback which must be a top level function or static function, that will be called by our plugin when any incoming or outgoing call events are detected.

@pragma('vm:entry-point') // Be sure to annotate your callback function to avoid issues in release mode on Flutter >= 3.3.0 void phoneStateBackgroundCallbackHandler(PhoneStateBackgroundEvent event, String number, int duration)

This callback handler must accept 3 arguments:

  • phoneStateBackgroundEvent: The event type detect by our plugin in background.

  • number: The incoming/outgoin number that is triggering the phone state call.

  • duration: An integer that represents the duration of the call in seconds.

The PhoneStateBackgroundEvent is an enum with four possible values:

Event Value Description
incomingstart Indicates an incoming call.
incomingmissed Indicates an incoming call missed.
incomingreceived Indicates an incoming call received.
incomingend Indicates an incoming call end.
outgoingstart Indicates an outgoing call start.
outgoingend Indicates an outgoing call end.

Since all this process happens in background in a Dart Isolate, there's no guarantee that the current OS will call the registered callback as soon as an event is triggered or that the callback will ever be called at all, each OS handle background services with different policies. Make sure to ask user permission before calling the PhoneStateBackground.initialize method of our plugin. Check the example to see a simple implementation of it.

/// Defines a callback that will handle all background incoming events
@pragma('vm:entry-point') // Be sure to annotate your callback function to avoid issues in release mode on Flutter >= 3.3.0
Future<void> phoneStateBackgroundCallbackHandler(
  PhoneStateBackgroundEvent event,
  String number,
  int duration,
) async {
  switch (event) {
    case PhoneStateBackgroundEvent.incomingstart:
      print('Incoming call start, number: $number, duration: $duration s');
      break;
    case PhoneStateBackgroundEvent.incomingmissed:
      print('Incoming call missed, number: $number, duration: $duration s');
      break;
    case PhoneStateBackgroundEvent.incomingreceived:
      print('Incoming call received, number: $number, duration: $duration s');
      break;
    case PhoneStateBackgroundEvent.incomingend:
      print('Incoming call ended, number: $number, duration $duration s');
      break;
    case PhoneStateBackgroundEvent.outgoingstart:
      print('Ougoing call start, number: $number, duration: $duration s');
      break;
    case PhoneStateBackgroundEvent.outgoingend:
      print('Ougoing call ended, number: $number, duration: $duration s');
      break;
  }
}

所属分类

源码仓库

使用此插件的项目

暂无相关数据。