即使屏幕关闭,我如何在后台运行代码? [英] How do I run code in the background, even with the screen off?

查看:24
本文介绍了即使屏幕关闭,我如何在后台运行代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Flutter 中有一个简单的计时器应用程序,它显示剩余秒数的倒计时.我有:

I have a simple timer app in Flutter, which shows a countdown with the number of seconds remaining. I have:

new Timer.periodic(new Duration(seconds: 1), _decrementCounter);

在我的手机显示屏关闭(即使我切换到另一个应用程序)并进入睡眠状态之前,它似乎工作正常.然后,计时器暂停.有没有推荐的方法来创建即使屏幕关闭也能在后台运行的服务?

It seems to work fine until my phone's display switches off (even if I switch to another app) and goes to sleep. Then, the timer pauses. Is there a recommended way to create a service that runs in the background even when the screen is off?

推荐答案

回答如何实现特定计时器案例的问题实际上与后台代码无关.在移动操作系统上不鼓励在后台运行整体代码.

Answering the question of how to implement your specific timer case doesn't actually have to do with background code. Overall running code in the background is something discouraged on mobile operating systems.

例如,iOS 文档在这里更详细地讨论了背景代码:https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

For example, iOS Documentation discusses background code in greater detail here: https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

相反,移动操作系统提供 API(如计时器/警报/通知 API)以在特定时间后回调您的应用程序.例如,在 iOS 上,您可以通过 UINotificationRequest 请求在将来的特定时间通知/唤醒您的应用程序:https://developer.apple.com/reference/usernotifications/unnotificationrequest这使他们能够终止/暂停您的应用以实现更好的节能效果,并拥有一个高效的共享系统服务来跟踪这些通知/警报/地理围栏等.

Instead mobile operating systems provide apis (like a timer/alarm/notification apis) to call back to your application after a specific time. For example on iOS you can request that your application be notified/woken at a specific point in the future via UINotificationRequest: https://developer.apple.com/reference/usernotifications/unnotificationrequest This allows them to kill/suspend your app to achieve better power savings and instead have a single highly-efficent shared system service for tracking these notifications/alarms/geofencing, etc.

Flutter 目前没有为这些 OS 服务提供任何开箱即用的包装器,但是使用我们的平台服务模型编写您自己的服务是直接的:flutter.io/platform-services

Flutter does not currently provide any wrappers around these OS services out-of-the-box, however it is straighforward to write your own using our platform-services model: flutter.io/platform-services

我们正在开发一种用于发布/共享此类服务集成的系统,以便一旦有人编写此集成(例如安排应用的某些未来执行),每个人都可以受益.

We're working on a system for publishing/sharing service integrations like this so that once one person writes this integration (for say scheduling some future execution of your app) everyone can benefit.

另外,是否可以运行后台 Dart 代码"(没有 FlutterView 在屏幕上处于活动状态)的更普遍的问题是还没有".我们在文件中有一个错误:https://github.com/flutter/flutter/issues/3671

Separately, the more general question of "is it possible to run background Dart code" (without having a FlutterView active on screen), is "not yet". We have a bug on file: https://github.com/flutter/flutter/issues/3671

驱动这种后台代码执行的用例是当您的应用收到通知时,想要使用一些 Dart 代码处理它而不将您的应用带到前台.如果您希望我们了解有关后台代码的其他用例,欢迎对该错误发表评论!

The use-case driving that kind of back-ground code execution is when your app receives a notification, wants to process it using some Dart code without bringing your app to the front. If you have other use cases for background code you'd like us to know about, comments are most welcome on that bug!

这篇关于即使屏幕关闭,我如何在后台运行代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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