如果我的 Android 应用程序没有 UI,我应该在单独的线程中工作吗?
我的应用程序本质上是一个在启动时启动的服务(由启动完成的接收器),并且应该定期收集数据,通过网络发送数据,然后进入睡眠状态(可能使用 AlarmManager)。它没有任何活动——没有任何用户界面。
是否有任何理由产生额外的线程来执行应用程序逻辑? 如果不是,我应该在哪里执行逻辑?在 OnStart 方法中?
My application is essentially a service that is started on boot (by a boot-completed receiver), and should periodically gather data, send it over the network, and go to sleep (probably using AlarmManager). It has no activities - no UI whatsoever.
Is there any reason to spawn an additional thread to perform the application logic?
if not, where should I perform the logic? in the OnStart method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
绝对地。如果您的服务在 5-10 秒内未能响应,您的服务将被终止。没有什么可以在这么长的时间内占用主应用程序线程。
我建议将
IntentService
与AlarmManager
一起使用。特别是,如果您希望设备在执行任何操作时保持唤醒状态,您可以考虑我的WakefulIntentService
。另外,关于“没有任何 UI”,如果您计划通过 Android Market 分发此应用程序,请记住,用户似乎不喜欢没有 UI 的应用程序。他们安装了它,当启动器中没有图标时会感到困惑,并给你一星评级。即使您不需要 UI 来进行实际操作,您也可以考虑至少在启动器中进行一些活动,显示文档,也许是正在完成的工作日志,允许调整工作频率等。
Absolutely. Your service will be killed off if it fails to respond within 5-10 seconds. Nothing can tie up the main application thread for that length of time.
I recommend an
IntentService
for use withAlarmManager
. In particular, if you want the device to stay awake while you are doing whatever it is you are doing, you might consider myWakefulIntentService
.Also, regarding "no UI whatsoever", if you plan on distributing this app via the Android Market, please bear in mind that users seem to dislike applications with no UI. They install it, get confused when there is no icon in the launcher, and give you a one-star rating. Even if you do not need a UI for actual operation, you might consider at least having some activity in the launcher, that shows documentation, perhaps a log of work being done, allows adjustment to the frequency of your work, etc.