MAUI: which one to use between Task.Run, Worker and Foreground service when I want to run a periodic process?

huangapple go评论69阅读模式
英文:

MAUI: which one to use between Task.Run, Worker and Foreground service when I want to run a periodic process?

问题

I have an Android application that is developed with MAUI.

我有一个使用MAUI开发的Android应用程序。

I want to run a process in the background of the application that checks periodically the location, every 5 minutes for example, and send a notification according to some condition. A notification or alarm sound.

我想在应用程序的后台运行一个进程,定期检查位置,例如每5分钟,根据某些条件发送通知或警报声音。

I would like to run this process also when the device reboots or powers on, as long as the user has set the application to auto-start.

我希望在设备重新启动或开机时也能运行此进程,只要用户设置了应用程序自动启动。

Also, I would like to run this process no matter if the application is closed or not in the foreground. I mean, if the device is on the desktop screen, locked, or running another application.

此外,我希望无论应用程序是否关闭或不在前台运行,都能运行此进程。我的意思是,如果设备位于桌面屏幕上、已锁定或正在运行另一个应用程序。

I am reading that to run a process in the background, I can use Task.Run (this is a cross-platform solution), a Worker, or a Foreground Service. These last two options are Android-specific solutions.

我了解到,要在后台运行进程,我可以使用Task.Run(这是一个跨平台解决方案)、Worker或Foreground Service。这两个选项是针对Android的解决方案。

But I don't find clear information that explains when to use each solution, or at least I don't understand well.

但我没有找到清晰的信息来解释何时使用每种解决方案,或者至少我不太理解。

For me, it seems that the easiest solution is to create a task that is an infinite while loop with the logic that I need and wait the required time. But this solution is part of the application, so I don't know if it will keep running even though the application is closed, or if it will run if the application is not in the foreground.

对我来说,似乎最简单的解决方案是创建一个无限循环的任务,其中包含我需要的逻辑,并等待所需的时间。但这个解决方案是应用程序的一部分,所以我不知道即使应用程序关闭它是否会继续运行,或者是否会在应用程序不在前台运行时运行。

In summary, I would like to know the difference between the different solutions and which would be the best option for my case.

总之,我想了解不同解决方案之间的区别,以及对于我的情况来说哪个选项最好。

英文:

I have an Android application that is developped with MAUI.

I want to run a proecss in the backgroud of the application that check periocally the location, every 5 minutes for example, and send a notification according to some condition. A notification or alarm o sound.

I would like to run this process also when the device reboot or power on, always that the user has setted that the application can auto start.

Also I would like to run this process no matter if the application is closed or is not in the foregound, I mean, if the device is in the desktop screen, locked or running another application.

I am reading that to run a process in the background, I can use a Task.Run (this is cross-platform solution), a Worker or a Foreground Service.This two last options are Android specfic solution.

But I don't find a clear information that explains when to use each solution, or at least I don't understand well.

For me, it seems that the easiest solution it is to create a task that is a infinite while loop, with the logic that I need and wait the requiered time. But this solution is part of the application, so I don't know if it will keep running althoung the application is closed. or if it will run if the application is not in the foreground.

In summary, I would like to know the difference between the different solutions and which it would be the best option for my case.

答案1

得分: 1

"Without further reading after your "I would like to run this process no matter if the application is closed or is not in the foregound" go for Foreground service you have no other options really, as Background service is too restrictive and Foreground service is 100% fine." can be translated as:

"在阅读之后,如果您想要在应用程序关闭或不在前台时运行此进程,只有一种选择,那就是前台服务,因为后台服务太受限制,而前台服务完全可行。"

英文:

Without further reading after your "I would like to run this process no matter if the application is closed or is not in the foregound" go for Foreground service you have no other options really, as Background service is too restrictive and Foreground service is 100% fine.

huangapple
  • 本文由 发表于 2023年5月7日 16:36:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76192902.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定