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

插件详情v2.0.1+1

foreground_service

用于通过 Android 前台服务运行 Dart 代码的 Flutter 插件。仅在 Android O 上进行了基本测试,在 M 上略有测试。不计划支持 iOS。

近 30 天下载
25
喜欢数
52
Pub 分数
30

安装插件

flutter pub add foreground_service

插件介绍

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

一个 Flutter 插件,使 Dart 代码能够在 Android 前台服务中运行,确保长时间运行的任务即使在应用处于后台时也能继续执行。主要针对 Android O 及更高版本设计,对旧版本的测试较少。不计划支持 iOS。该插件与 Android 的前台服务生命周期集成,允许应用在系统终止前持续进行关键操作,如位置跟踪或音频播放。需要在 Android 清单文件中正确配置和声明权限。

适合场景

  • 背景位置跟踪
  • 长时间运行的数据处理
  • 后台音频播放
  • 持久的网络连接
  • 实时监控服务

主要特点

  • Android 前台服务集成
  • Dart代码在前台上下文中执行
  • 系统级持久化
  • 最小 iOS 支持
  • 需要配置清单

适合对象

  • 需要后台任务可靠性的应用
  • 基于位置的服务
  • 媒体播放器
  • 物联网设备控制器
  • 实时数据同步

使用建议

  • 无 iOS 支持
  • 在较旧的 Android 版本上进行了有限的测试
  • 需要 Android 清单设置
  • 需要前台服务通知
  • 电池使用影响

原始 README

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

foreground_service (Flutter v.1.12.x or later)

Create Android foreground service/notification

Prep (Android side):

Android foreground services require a notification to be displayed,
and notifications require an icon.

For this plugin to work, the icon needs to be in this specific location:

res/drawable/org_thebus_foregroundserviceplugin_notificationicon

(take a look at the /example app if you're confused)


-- NOTE FOR PLUGIN VERSION 2.0.0 and above --
compileSdkVersion & targetSdkVersion need to be 29 or above, or the build will fail
these values are set/can be changed in the build.gradle for the app

Use (Flutter/Dart side):

To start the service, call ForegroundService.startForegroundService([serviceFunction])

serviceFunction will then be executed periodically, but "minimum/best-effort"
i.e. it will try to make the interval between function executions *at least* that long

Doesn't work?

As long as you're calling ForegroundService.startForegroundService,
"flutter run" should show error messages that indicate what's wrong/missing

i.e. messages beginning with E/ForegroundServicePlugin indicate an error from the plugin

Caution:

ForegroundService.notification.get* methods may give unexpected values.

Once notifications are sent out, there's no way to retrieve the "current" data.

To work around this, the plugin keeps a version of the notification around.
This version may not have been "sent out" yet, however.

Disclaimer:

Most of the fancy stuff is shamelessly pilfered from the android_alarm_manager plugin

所属分类

源码仓库

使用此插件的项目

暂无相关数据。