搜尋套件、儲存庫與 APP 分類

套件詳情v0.4.0

background_task

應用進入背景後,可使用位置更新進行處理。

AndroidiOS
近 30 天下載
97
喜歡數
40
Pub 分數
160

安裝套件

flutter pub add background_task

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

background_task 套件讓 Flutter 應用程式能在背景執行作業,特別是位置更新。此套件專為 Android 與 iOS 設計,讓開發者能持續維持功能,例如追蹤使用者移動而不中斷。該套件與原生平台功能整合,以確保在背景狀態下可靠執行。對於需要即時位置監控或定期資料收集的應用程式尤為有用。此解決方案具備最小開銷,並透過專用的原生實作支援兩個平台。

適用情境

  • 即時位置追蹤
  • 週期性資料同步
  • 背景地理圍欄
  • 長時間執行的任務
  • 省電的背景處理

主要特色

  • 支援 Android 與 iOS
  • 處理背景中的位置更新
  • 最小化電池影響
  • 原生平台整合
  • 任務排程的簡單 API

適合對象

  • 健身與導航應用
  • 物流與配送追蹤
  • 現場服務管理
  • 需要持續背景活動的應用程式
  • 基於位置的服務

使用建議

  • 需要在兩個平台上都有適當的權限
  • 可能受作業系統省電模式限制
  • 需要在實際設備上進行測試
  • 背景執行限制因作業系統版本而異
  • 使用者必須授與位置存取權限

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

background_task

never-light-log

Developed with 💙 by Never inc.


Motivation

Enable developers to continue processing even when the application transitions to the background, we have created a package that allows processing to continue using location updates.This package was created with reference to background_location.

Can be used when you want to run the program periodically in the background.

  • Monitor and notify the distance walked and steps.
  • Notification of destination arrival.
  • Tracking location information (sending it to a server).

Usage

// Monitor notifications of background processes.
// However, Cannot be used while the app is in task kill.
BackgroundTask.instance.stream.listen((event) {
  // Implement the process you want to run in the background.
  // ex) Check health data.
});

// Start background processing with location updates.
await BackgroundTask.instance.start();

// Stop background processing and location updates.
await BackgroundTask.instance.stop();

This is an implementation for receiving updates even when the task is task-killed. In this package, iOS uses startMonitoringSignificantLocationChanges and Android uses ForegroundService.

// Define callback handler at the top level.
@pragma('vm:entry-point')
void backgroundHandler(Location data) {
  // Implement the process you want to run in the background.
  // ex) Check health data.
}

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  BackgroundTask.instance.setBackgroundHandler(backgroundHandler); // 👈 Set callback handler.
  runApp(const MyApp());
}

To get the latest location information in a task-killed status, set the app to Always.

ios android

This is an implementation for when you want to stop using the application when it is killed.

await BackgroundTask.instance.start(
  isEnabledEvenIfKilled: false,
);

Recommended to use with permission_handler.

final status = await Permission.location.request();
final statusAlways = await Permission.locationAlways.request();

if (status.isGranted && statusAlways.isGranted) {
  await BackgroundTask.instance.start();
}
Setup

pubspec.yaml

dependencies:
  background_task:

iOS: Info.plist

<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Used to monitor location in the background and notify to app.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Used to monitor location in the background and notify to app.</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Used to monitor location in the background and notify to app.</string>
<key>UIBackgroundModes</key>
<array>
    <string>fetch</string>
    <string>location</string>
</array>

To use an external package (shared_preference etc..) in callback handler, register DispatchEngine in AppDelegate.

iOS: AppDelegate.swift

import UIKit
import Flutter
import background_task // 👈 Add

@main
@objc class AppDelegate: FlutterAppDelegate, FlutterImplicitEngineDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
      GeneratedPluginRegistrant.register(with: self)
      // 👇 Add
      BackgroundTaskPlugin.onRegisterDispatchEngine = {
          GeneratedPluginRegistrant.register(with: BackgroundTaskPlugin.dispatchEngine)
      }
      return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }

  func didInitializeImplicitFlutterEngine(_ engineBridge: FlutterImplicitEngineBridge) {
    GeneratedPluginRegistrant.register(with: engineBridge.pluginRegistry)
  }
}

Android: AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>

References

所屬分類

原始碼儲存庫

使用此套件的專案

暫無相關資料。