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

套件詳情v1.3.1

flutter_background

在 Android 上使用前景服務於背景執行應用程式。適用於需要持續運作的應用程式,例如背景資料同步或訊息傳遞。

Android
近 30 天下載
4.7萬
喜歡數
342
Pub 分數
160

安裝套件

flutter pub add flutter_background

套件介紹

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

flutter_background 套件讓 Android 應用能在前景中運行並使用持續性服務,確保即使應用未使用時也能持續運作。它非常適合需要不斷執行的任務,例如背景資料同步、即時通訊或定位追蹤。透過利用 Android 的前景服務功能,可防止作業系統因記憶體管理而終止應用。此套件與 Flutter 完全整合,僅支援 Android 平台。

適用情境

  • 背景數據同步
  • 即時訊息
  • 位置追蹤
  • 長時間執行的任務
  • 持久化服務執行

主要特色

  • 前臺服務支援
  • 僅限 Android 平台
  • 持續的背景執行
  • 與 Flutter 的輕鬆整合
  • 防止應用終止

適合對象

  • 需要持續後台運行的應用程式
  • 開發訊息傳遞或同步功能的開發者
  • 需要可靠前臺服務的專案
  • 面向Android的移動應用程式
  • 應用程式穩定性至關重要的使用情境

使用建議

  • 僅限 Android
  • 需要使用者權限以啟用前臺服務
  • 可能影響電池壽命
  • 需要正確處理生命週期事件
  • 與 iOS 不相容

原始 README

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

flutter_background

"Buy Me A Coffee"

A plugin to keep flutter apps running in the background. Currently only works with Android.

It achieves this functionality by running an Android foreground service in combination with a partial wake lock and disabling battery optimizations in order to keep the flutter isolate running.

Note: This plugin currently only works with Android. PRs for iOS are very welcome, although I am not sure if a similar effect can be achieved with iOS at all.

Getting started

To use this plugin, add flutter_background as a dependency in your pubspec.yaml file.

Android

Add the following permissions to the AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="de.julianassmann.flutter_background_example">

    <!-- Adapt to the foreground service type(s) desired, these are just examples -->
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />

    <application>
        ...

        <!-- Adapt to the foreground service type(s) desired, these are just examples -->
        <service
            android:name="de.julianassmann.flutter_background.IsolateHolderService"
            android:exported="false"
            android:foregroundServiceType="dataSync|specialUse|..." />
    </application>
</manifest>

Important: You must specify the appropriate foregroundServiceType for your use case. See the Android docs and the list of available service typs for more information on foreground service types.

Other platforms

Android is the only supported platform. There are currently no plans to extend support for other platforms, but feel free to change that by contributing to this plugin.

Usage

Import flutter_background.dart:

import 'package:flutter_background/flutter_background.dart';
Initializing plugin and handling permissions

Before you can use this plugin, you need to initialize it by calling FlutterBackground.initialize(...):

final androidConfig = FlutterBackgroundAndroidConfig(
    notificationTitle: "flutter_background example app",
    notificationText: "Background notification for keeping the example app running in the background",
    notificationImportance: AndroidNotificationImportance.normal,
    notificationIcon: AndroidResource(name: 'background_icon', defType: 'drawable'), // Default is ic_launcher from folder mipmap
);
bool success = await FlutterBackground.initialize(androidConfig: androidConfig);

This ensures all permissions are granted and requests them if necessary. It also configures the foreground notification. The configuration above results in the foreground notification shown below when running FlutterBackground.enableBackgroundExecution().

The foreground notification created by the code above.

The arguments are:

  • notificationTitle: The title used for the foreground service notification.
  • notificationText: The body used for the foreground service notification.
  • notificationImportance: The importance of the foreground service notification.
  • notificationIcon: The icon used for the foreground service notification shown in the top left corner. This must be a drawable Android Resource (see here for more). E. g. if the icon with name "background_icon" is in the "drawable" resource folder, it should be of value `AndroidResource(name: 'background_icon', defType: 'drawable').
  • enableWifiLock: Indicates whether or not a WifiLock is acquired when background execution is started. This allows the application to keep the Wi-Fi radio awake, even when the user has not used the device in a while (e.g. for background network communications).

In this example, background_icon is a drawable resource in the drawable folders (see the example app). For more information check out the Android documentation for creating notification icons for more information how to create and store an icon.

In order to function correctly, this plugin needs a few permissions. FlutterBackground.initialize(...) will request permissions from the user if necessary. You can call initialize more than one time, so you can call initalize() every time before you call enableBackgroundExecution() (see below).

In order to notify the user about upcoming permission requests by the system, you need to know, whether or not the app already has these permissions. You can find out by calling

bool hasPermissions = await FlutterBackground.hasPermissions;

before calling FlutterBackground.initialize(...). If the app already has all necessary permissions, no permission requests will be displayed to the user.

Run app in background

With

bool success = await FlutterBackground.enableBackgroundExecution();

you can try to get the app running in the background. You must call FlutterBackground.initialize() before calling FlutterBackground.enableBackgroundExecution().

With

await FlutterBackground.disableBackgroundExecution();

you can stop the background execution of the app. You must call FlutterBackground.initialize() before calling FlutterBackground.disableBackgroundExecution().

To check whether background execution is currently enabled, use

bool enabled = FlutterBackground.isBackgroundExecutionEnabled;

Example

The example is a TCP chat app: It can connect to a TCP server and send and receive messages. The user is notified about incoming messages by notifications created with the plugin flutter_local_notifications.

Using this plugin, the example app can maintain the TCP connection with the server, receiving messages and creating notifications for the user even when in the background.

Maintainer

Julian Aßmann

If you experience any problems with this package, please create an issue on Github. Pull requests are also very welcome.

所屬分類

依賴

依賴清單 1 項

  • plugin_platform_interface^2.1.8

原始碼儲存庫

使用此套件的專案

所在榜單