简体   繁体   English

Xamarin Forms (Android) - 后台服务与 Task.Run 方法

[英]Xamarin Forms (Android) - Background Service vs. Task.Run Method

I have created an app with Xamarin Form where a timer runs in the background.我使用 Xamarin 表单创建了一个应用程序,其中计时器在后台运行。 I used the Task.Run method for this and so far everything works normally when the display is not closed / turned off.我为此使用了 Task.Run 方法,到目前为止,当显示器未关闭/关闭时一切正常。 When the display is closed then the timer does not run.当显示器关闭时,计时器不会运行。 I have informed myself that for this you should use the Background Service but I don't understand the difference between Task.Run method and Background Service.我已经告诉自己,为此您应该使用后台服务,但我不明白 Task.Run 方法和后台服务之间的区别。 Thanks for the answer感谢你的回答

Service服务

The primary mechanism for background work in Android is the service. Android 中后台工作的主要机制是服务。 An Android service is a component that is designed to do some work without a user interface. Android 服务是一个组件,旨在在没有用户界面的情况下完成某些工作。 A service might download a file, play music, or apply a filter to an image.服务可能会下载文件、播放音乐或对图像应用过滤器。

Background work can be broken down into two broad classifications:背景工作可以分为两大类:

1.Long Running Task – This is work that is ongoing until explicitly stopped. 1.Long Running Task——这是一直持续到明确停止的工作。 An example of a long running task is an app that streams music or that must monitor data collected from a sensor.长时间运行任务的一个示例是流式传输音乐或必须监控从传感器收集的数据的应用程序。 These tasks must run even though the application has no visible user interface.即使应用程序没有可见的用户界面,这些任务也必须运行。

2.Periodic Tasks – (sometimes referred to as a job) A periodic task is one that is of relatively short in duration (several seconds) and is run on a schedule (ie once a day for a week or perhaps just once in the next 60 seconds). 2.Periodic Tasks -(有时称为作业)周期性任务是持续时间相对较短(几秒)并按计划运行的任务(即每天一次,持续一周,或者可能只在接下来的一次60 秒)。 An example of this is downloading a file from the internet or generating a thumbnail for an image.这方面的一个示例是从 Internet 下载文件或为图像生成缩略图。

For more details, you can check: https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/services/ .有关更多详细信息,您可以查看: https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/services/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM