如何在Flutter中安排后台任务? [英] How to schedule background tasks in Flutter?

查看:612
本文介绍了如何在Flutter中安排后台任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在为此寻找很多东西,但是在Flutter中找不到任何软件包或安排后台任务的方法。就像在Android中一样,有 WorkManager AlarmManager

I have been looking a lot for this but haven't found any packages or a way to schedule background tasks in Flutter. Like in Android there is WorkManager,AlarmManager.

我知道我可以使用 MethodChannel 访问这些类,但是我想要一些适用于iOS和Android的东西。

I know I can access those classes using MethodChannel, but I want something that works for iOS and Android both.

(非常令人失望的是,移动框架无法安排后台任务)。

(Its very disappointing that a mobile framework doesn't have the ability to schedule background tasks).

推荐答案

有一个中型博客文章,说明了如何执行此操作。

但是我们认为设置起来太复杂了,所以我们创建了插件可以帮助您。

There is a Medium blogpost that explains how to do this.
However we thought it was way too complicated to set up so it just happens we created a plugin that aids you with this.

//Provide a top level function or static function.
//This function will be called by Android and will return the value you provided when you registered the task.
//See below
void callbackDispatcher() {
  Workmanager.executeTask((task) {
    print("Native echoed: $task");
    return Future.value(true);
  });
}

Workmanager.initialize(
    callbackDispatcher, //the top level function.
    isInDebugMode: true //If enabled it will post a notification whenever the job is running. Handy for debugging jobs
)

我们支持Android的Workmanager和iOS performFetch

We support Android's Workmanager and iOS performFetch

目前,它仅适用于Android项目,但我们很快就会着眼于iOS。

For now it only works for Android project, but we are looking at iOS soon.
I'll update this answer when it is available.

我们现在也有iOS支持。它仍然是早期的alpha版本,但请放手。

我们写了一个免费的帖子也是如此。

We have iOS support now too. It is still early alpha, but give a go.
We wrote a complimentary Medium post too.

这篇关于如何在Flutter中安排后台任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆