패키지, 저장소 및 앱 카테고리 검색

패키지 상세v0.4.0

background_task

앱이 백그라운드로 전환된 후 위치 업데이트를 사용하여 처리할 수 있습니다.

AndroidiOS
30일 다운로드
115
좋아요
40
Pub 점수
160

패키지 설치

flutter pub add background_task

패키지 개요

공개 정보를 바탕으로 생성한 선택 가이드입니다.

background_task 패키지는 플러터 애플리케이션이 백그라운드에서도 작업(특히 위치 업데이트)을 수행할 수 있도록 합니다. 안드로이드 및 iOS를 위한 것으로, 사용자 이동 추적과 같은 지속적인 기능을 중단 없이 유지할 수 있게 해줍니다. 네이티브 플랫폼 기능과 통합되어 백그라운드 상태에서도 신뢰성 있는 실행을 보장합니다. 실시간 위치 모니터링 또는 주기적인 데이터 수집이 필요한 앱에 특히 유용합니다. 이 솔루션은 최소한의 오버헤드로 구축되었으며, 전용 네이티브 구현을 통해 두 플랫폼 모두를 지원합니다.

사용 사례

  • 실시간 위치 추적
  • 주기적인 데이터 동기화
  • 백그라운드 지오페싱
  • 장시간 실행 작업 실행
  • 배터리 효율적인 백그라운드 처리

주요 기능

  • Android 및 iOS를 지원합니다
  • 백그라운드에서 위치 업데이트 처리
  • 배터리 영향 최소화
  • 네이티브 플랫폼 통합
  • 작업 스케줄링을 위한 간단한 API

적합한 대상

  • 피트니스 및 탐색 앱
  • 물류 및 배송 추적
  • 현장 서비스 관리
  • 지속적인 백그라운드 활동이 필요한 앱
  • 위치 기반 서비스

고려 사항

  • 두 플랫폼 모두에서 적절한 권한이 필요합니다
  • 운영체제의 전력 절약 모드에 의해 제한될 수 있습니다
  • 실기기에서 테스트 필요
  • 백그라운드 실행 제한은 OS 버전에 따라 다릅니다
  • 사용자는 위치 액세스 권한을 부여해야 합니다

원본 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

카테고리

소스 저장소

이 패키지를 사용하는 프로젝트

아직 데이터가 없습니다.