关于flutter:如何在-Flutter-创建一个后台任务

1次阅读

共计 2891 个字符,预计需要花费 8 分钟才能阅读完成。

原文

https://www.dltlabs.com/blog/…

参考

  • https://pub.dev/packages/back…

注释

明天,我将解释如何在 Flutter 创立一个后台任务。

在此之前,让咱们了解什么是后台任务。后台任务是在后盾运行的应用程序的辅助过程,即便应用程序没有运行或处于终止状态。

这一性能对于须要在后盾执行工作而不须要用户关上应用程序的应用程序来说是无益的ーー例如,每 15 分钟调用 api 获取数据。

让咱们在一个示例我的项目中实现一个后台任务,以便更好地了解这一操作的含意。

步骤:

  • pubspec.yaml
flutter pub add background_fetch
flutter pub get
  • 在 main.dart 文件中导入后盾包,并注册 HeadlessTask,以便在应用程序终止后接管 backgroundFetch 事件。

例如:

void backgroundFetchHeadlessTask(HeadlessTask task) async {var taskId = task.taskId;
if(taskId ==‘your_task_id’) {print(‘your_task_id’);
print(‘[BackgroundFetch] Headless event received.’);
_//TODO: perform tasks like — call api, DB and local notification etc…
_}
}
void main() {runApp(MyApp());
_//Registering backgroundFetch to receive events after app is terminated.
// Requires {stopOnTerminate: false, enableHeadless: true}
_BackgroundFetch._registerHeadlessTask_(backgroundFetchHeadlessTask);
}

这里咱们必须传递一个顶级函数。让咱们在 registerHeadlessTask 办法中给它命名为 call back dispatcher。而后咱们定义须要在后盾运行的工作:

配置 BackgroundFetch

Future<void> initPlatformState() async {
_// Configure BackgroundFetch.
_var status = await BackgroundFetch._configure_(BackgroundFetchConfig(
minimumFetchInterval: 15,
forceAlarmManager: false,
stopOnTerminate: false,
startOnBoot: true,
enableHeadless: true,
requiresBatteryNotLow: false,
requiresCharging: false,
requiresStorageNotLow: false,
requiresDeviceIdle: false,
requiredNetworkType: NetworkType.NONE,
), _onBackgroundFetch, _onBackgroundFetchTimeout);
print(‘[BackgroundFetch] configure success: $status’);
_// Schedule backgroundfetch for the 1st time it will execute with 1000ms delay.
// where device must be powered (and delay will be throttled by the OS).
_BackgroundFetch.scheduleTask(TaskConfig(
taskId:“com.dltlabs.task”,
delay: 1000,
periodic: false,
stopOnTerminate: false,
enableHeadless: true
));
}

调用 initState 中的 initPlatformState 办法并设置 BackgroundFetchConfig 类的配置。换句话说,在传递其余参数的同时,提供注册一次性工作或周期性工作的选项。

在这里,如果存在多个工作,工作 id 能够帮忙咱们轻松地辨认单个工作。输出数据能够是解决工作所需的任何信息。

如果咱们心愿在应用程序处于终止状态时持续工作,请将 stopOnTerminate 参数的值设置为 false。如果 stopOnTerminate 设置为 true,后盾服务将在应用程序终止时终止。

void _onBackgroundFetchTimeout(String taskId) {print(“[BackgroundFetch] TIMEOUT: $taskId”);
BackgroundFetch.finish(taskId);
}

当操作系统没有执行后台任务或者工作无奈在给定工夫内运行时,将调用 onBackgroundFetchTimeout 办法。在这种办法中,咱们能够用工作 Id 来解决工作。

void _onBackgroundFetch(String taskId) async {if(taskId ==‘your_task_id’) {print(‘[BackgroundFetch] Event received’);
//TODO: perform your task like : call the API’s, call the DB and local notification.
}
}

当后盾服务执行事件时,将调用 onBackgroundFetch 办法。在这个办法中,咱们将接管工作 id 作为一个参数,它能够用来解决工作。在须要调用 api 将数据保留到数据库或显示本地告诉等状况下,这一点十分重要。

默认状况下,调用工作之后的工夫距离的频率为 15 分钟。如果你想把它设置成其余的货色,你也能够在这里做。在 Android 上,后盾过程的最小工夫距离为 15 分钟。如果该值小于 15,Android 默认应用 15 分钟。

Also, we must also remember to make changes inside the info.plist and manifest.xml file for both iOS \& Android. We need to set some of the permissions, and we also need to copy and paste other settings. If you need these settings, you can get them at the following links: Android , OS.

此外,咱们还必须记住对 Android IOS 的 info.plistmanifest.xml 文件进行更改。咱们须要设置一些权限,还须要复制和粘贴其余设置。如果你须要这些设置,你能够通过以下链接取得: IOS , Android。

谢谢你的浏览!


© 猫哥

  • 微信 ducafecat
  • 博客 ducafecat.tech
  • github
  • bilibili

正文完
 0