搜尋套件、儲存庫與 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;
  }
}

所屬分類

原始碼儲存庫

使用此套件的專案

暫無相關資料。