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

插件详情v1.6.1

background_location_tracker

一个 Flutter 插件,可让您在 Android 和 iOS 上跟踪后台位置

AndroidiOS
近 30 天下载
830
喜欢数
76
Pub 分数
150

安装插件

flutter pub add background_location_tracker

插件介绍

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

一个 Flutter 插件,可在 Android 和 iOS 上实现后台持续定位跟踪。它允许应用程序在应用未使用时仍能监控用户位置,非常适合用于健身、导航或物流类应用。该插件处理平台特定的配置和权限,确保在各种设备上可靠运行。它与 Flutter 生态系统无缝集成,并支持实时更新,同时对电池影响极小。

适合场景

  • 健身追踪应用
  • 交付与物流监控
  • 地理围栏应用
  • 旅行和导航工具
  • 基于位置的提醒

主要特点

  • 后台位置更新
  • 跨平台支持(Android 和 iOS)
  • 低电池消耗
  • 自动权限处理
  • 实时位置流

适合对象

  • 需要持续位置跟踪的应用程序
  • 开发位置感知服务的开发者
  • 交通或健康领域的移动应用

使用建议

  • 需要用户明确授权
  • 可能影响设备电池寿命
  • 需要平台特定的设置
  • 隐私合规要求
  • iOS 上的后台执行限制

原始 README

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

background_location_tracker

A new Flutter plugin that allows you to track the background location for Android & iOS

Build Status Coverage Status pub package

Android Config

Update compile sdk

Compile sdk should be at 29 at least.

android {
  ...
  compileSdkVersion 29
  ...

  defaultConfig {
    ...
    targetSdkVersion 29
    ...
  }
  ...
}

iOS Configuration

Update Info.plist

Add the correct permission descriptions

	<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
	<string>Your description why you should use NSLocationAlwaysAndWhenInUseUsageDescription</string>
	<key>NSLocationAlwaysUsageDescription</key>
	<string>Your description why you should use NSLocationAlwaysAndWhenInUseUsageDescription</string>
	<key>NSLocationWhenInUseUsageDescription</key>
	<string>Your description why you should use NSLocationAlwaysAndWhenInUseUsageDescription</string>

Add the background location updates in xcode

Or add the info to the Info.plist

	<key>UIBackgroundModes</key>
	<array>
		<string>location</string>
	</array>
Update the AppDelegate
import UIKit
import Flutter
import background_location_tracker

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
    override func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        GeneratedPluginRegistrant.register(with: self)

        // Register plugins for background execution - this method is deprecated 
        // but kept for backward compatibility
        BackgroundLocationTrackerPlugin.setPluginRegistrantCallback { registry in
            GeneratedPluginRegistrant.register(with: registry)
        }

        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }
}

Flutter implementation

Make sure you set the @pragma('vm:entry-point') to make sure you can find the callback in release.

@pragma('vm:entry-point')
void backgroundCallback() {
  BackgroundLocationTrackerManager.handleBackgroundUpdated(
    (data) async => Repo().update(data),
  );
}

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await BackgroundLocationTrackerManager.initialize(
    backgroundCallback,
    config: const BackgroundLocationTrackerConfig(
      loggingEnabled: true,
      androidConfig: AndroidConfig(
        notificationIcon: 'explore',
        trackingInterval: Duration(seconds: 4),
        distanceFilterMeters: null,
      ),
      iOSConfig: IOSConfig(
        activityType: ActivityType.FITNESS,
        distanceFilterMeters: null,
        restartAfterKill: true,
      ),
    ),
  );

  runApp(MyApp());
}

Future startLocationTracking() async {
  await BackgroundLocationTrackerManager.startTracking();
}

Future stopLocationTracking() async {
  await BackgroundLocationTrackerManager.stopTracking();
}

FAQ:

I get a Unhandled Exception: MissingPluginException(No implementation found for method .... on channel ...)
This is mostly caused by a misconfiguration of the plugin:
- Android: This plugin now uses Flutter's Plugin V2 embedding pattern (FlutterPluginBinding) so you don't need to set a manual plugin registrant callback.
- iOS: For compatibility with older Flutter versions, the setPluginRegistrantCallback is still available but will be removed in a future version.

If you're using Flutter 1.12 or later, the plugin should work out of the box with the V2 embedding.

Migration Guide

Upgrading from older versions

This plugin has been updated to use the modern Flutter Plugin V2 embedding pattern with FlutterPluginBinding. If you're using Flutter 1.12 or later, you don't need to make any changes to your app's code.

For backward compatibility, the older methods are still available but marked as deprecated:

  • BackgroundLocationTrackerPlugin.setPluginRegistrantCallback on Android
  • BackgroundLocationTrackerPlugin.setPluginRegistrantCallback on iOS

These methods will be removed in a future version of the plugin.

所属分类

源码仓库

使用此插件的项目

暂无相关数据。