Android:必须手动启动的后台处理组件
我需要你的想法。我正在寻找的是一个任务或服务,在后台运行(无用户界面),能够执行以下操作:
- 它不应该永久运行,只有在被调用时才运行。恰好有两个可能的调用者:
- GUI/触摸应用程序。用户可以输入数据,应用程序会将这些数据传递到后台服务,在后台服务中数据会被持久保存。这可能随时随机发生
- Android 操作系统,定期。也许 AlarmManager 是一个合适的方法。当服务被定期调用(例如每 30 秒)时,它应该处理步骤 1 中存储的数据。处理完成后,它应该自行停止以节省电池。
换句话说: 用户不触发处理,用户只负责输入。 AlarmManager 然后定期触发处理。
android 服务类是否适合于此,或者有更好的方法吗?
如果是,则我在理解服务类别时遇到问题。根据文档,它没有自己的进程,而是在主机进程中运行。这会导致问题吗?所以有时phonegap插件(传递数据)是主机进程,有时警报代码是主机进程,还是我错了?我的意思是该服务没有主要应用程序,它是独立的东西,可以从不同的位置调用。
谢谢
谢谢
I need your thoughts. What I'm looking for is a task or service, running in the background (no UI), capable of the following:
- it should not run permanently, only when it gets called. There are exactly two possible callers:
- a gui/touch application. Users can enter data and the application passes this data to the background service where the data gets persistet. This can happen at any time, randomly
- the Android OS, periodically. Maybe the AlarmManager is a suitable approach. When the service gets called periodically (e.g. each 30 seconds), it should process the data that was stored in step 1. When the processing is finished, it should stop itself to save battery.
In other words:
The user does not trigger the processing, the user is just responsible for the input. The AlarmManager then triggers the processing periodically.
Is the android service class suitable for that or is there a better way to go?
If yes, I have a problem understanding the service class. According to the docs, it does not have its own process but runs in the host process. Could this lead to problems? So sometimes the phonegap plugin (which passes the data) is the host process and sometimes the alarm code is the host process, or I am wrong? I mean the service has no main application, it is rather something independent that can be called from different locations.
Thanks
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里提供服务是合适的。
您可以安排 AlarmManager 定期调用您的服务,让您的 UI 活动向其推送数据,并可选择唤醒服务来处理数据。
服务在宿主进程中运行,并且将在 UI 线程中运行,除非您创建另一个线程。
IntentService 类可以帮助您解决此问题。
它允许您触发由工作线程中的服务处理的意图。
意图在 IntentService 类中排队,并由 IntentService 管理的单个工作线程依次处理。
在您的情况下,您可以让 AlarmManager 触发一个 Intent,该 Intent 将由您的服务在工作线程中处理。
A service would be appropriate here.
You can schedule AlarmManager to call your Service at intervals and have your UI Activity push data to it and optionally wake the service up to process the data.
A Service runs in the host process and will run in the UI thread unless you create another thread.
The IntentService class can help you with this.
It allows you to fire intents off that get handled by the service in a worker thread.
The intents are queued up in the IntentService class and are processed in turn by a single worker thread that is managed by IntentService.
In your case, you can have the AlarmManager fire off an Intent that would be handled by your service in the worker thread.
请注意,一旦手机重新启动,
AlarmManager
就会丢失所有设置的闹钟,为了再次初始化闹钟,通常会创建一个从 BroadcastReceiver 派生的 OnBootReceiver,以便在手机重新启动时收到通知。那么你就必须再次设置闹钟。please consider that
AlarmManager
gets lost of all alarms set once the phone is rebooted, to initialise the alarms again it is common to create an OnBootReceiver derived from BroadcastReceiver to get notified when the phone was rebootet. then you'll have to set the alarms again.