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

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

问题描述

我在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服务的现成包装,但是使用我们的Platform-Services模型编写您自己的包装很简单: 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天全站免登陆