如果我的 Android 应用程序没有 UI,我应该在单独的线程中工作吗?

发布于 2024-10-04 01:44:33 字数 170 浏览 1 评论 0原文

我的应用程序本质上是一个在启动时启动的服务(由启动完成的接收器),并且应该定期收集数据,通过网络发送数据,然后进入睡眠状态(可能使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故人如初 2024-10-11 01:44:33

是否有任何理由产生额外的线程来执行应用程序逻辑?

绝对地。如果您的服务在 5-10 秒内未能响应,您的服务将被终止。没有什么可以在这么长的时间内占用主应用程序线程。

我建议将 IntentServiceAlarmManager 一起使用。特别是,如果您希望设备在执行任何操作时保持唤醒状态,您可以考虑我的 WakefulIntentService

另外,关于“没有任何 UI”,如果您计划通过 Android Market 分发此应用程序,请记住,用户似乎不喜欢没有 UI 的应用程序。他们安装了它,当启动器中没有图标时会感到困惑,并给你一星评级。即使您不需要 UI 来进行实际操作,您也可以考虑至少在启动器中进行一些活动,显示文档,也许是正在完成的工作日志,允许调整工作频率等。

Is there any reason to spawn an additional thread to perform the application logic?

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 with AlarmManager. In particular, if you want the device to stay awake while you are doing whatever it is you are doing, you might consider my WakefulIntentService.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文