创建一项服务来为活动付出所有努力是个好主意吗?
创建一个为 Activity 完成所有艰苦工作的服务是一个好主意吗? IE,活动将只关心接口和调用服务,以及它必须需要的其他功能,并且服务将是为活动工作的“工具”。
Is this a good idea to create a Service that make all the hard work for the Activity. IE, the Activity will just care about the interface and for calling the services, and other functionalities it must need, and the service will be the 'tool' that will work for the Activity.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,它是...我建议您观看此视频:
开发 Android REST 客户端应用程序
该视频讨论了 REST 客户端应用程序,但相同的技术已被证明适用于许多其他情况。
Yes, it's... I recommend you to watch this video:
Developing Android REST client applications
That video talks about REST client apps, but the same technique has been proved to work on many other situations.
嗯。封装对数据的访问可能是一个好主意,特别是因为它使测试变得更容易,并为您提供了缓存数据的机会。但不要过度设计。克里斯蒂安链接的视频中概述的方法对于许多(大多数?)应用程序来说是大材小用。在许多情况下,好的 AsyncTask 对于大多数“艰苦的工作”来说已经足够了。如果没有,那么一定要考虑 IntentService 作为您的下一个选择。在使用 AIDL 和绑定完成完整的服务路线之前,我会考虑这两个选项。如果我需要后台服务(用于通知或数据同步等)并且它与主应用程序共享数据(通常是这种情况),我通常只会选择后一条路线。至于视频中的 ContentProvider 模式..就我个人而言,我认为只有当您不想控制数据缓存(或者数据不被缓存,并且设备实际上是记录系统)并且您想要将数据公开给其他应用程序时,这些才值得做。我几乎总是倾向于将我的应用程序代码暴露给服务接口而不是游标。说真的,不要过度设计。
Meh. Encapsulating access to data can be a good idea, especially since it makes it easier to test and gives you an opportunity to cache data. But don't over-engineer. The approach outlined in the video linked by Cristian is overkill for many (most?) apps. In many cases good 'ol AsyncTask is good enough for most "hard work". If not, then definitely consider an IntentService as your next option. I would consider both of those options before going the full Service route complete with AIDL and bindings. I usually only go the latter route if I need a background Service (for Notifications or data-sync, etc.) and it shares data with the main App (which is often the case.) As for the ContentProvider patterns from the video ... Personally I only think these are worth doing if you don't want to control the caching of data (or the data is not cached, and the device is really the system of record) and you want to expose the data to other applications. I would almost always favor exposing my App code to a Service interface instead of a Cursor. Seriously, don't over-engineer.