简体   繁体   English

我应该使用哪种类型的Android服务?

[英]Which type of Android Services should I use?

I try to get services into my weather application. 我尝试将服务纳入我的天气应用程序。 Let's assume that user has set up locations (cities list), and that he tap an Update button. 假设用户已经设置了位置(城市列表),并且他点击了“更新”按钮。 In my case, this tap is caught by MainActivity . 就我而言,此水龙头被MainActivity捕获。 After that, the application should read from sqlite user locations to update weather data, loop through this list of locations, for each of them send request to weather provider, parse json and put weather data into database. 之后,应用程序应从sqlite用户位置读取信息以更新天气数据,循环浏览此位置列表,因为每个用户都向天气提供者发送请求,解析json并将天气数据放入数据库中。 On success, the current view should be updated to show data that was just received. 成功后,应更新当前视图以显示刚刚接收到的数据。 How should I separate all this logic between MainActivity and services, and which type of services should I use? 如何在MainActivity和服务之间分隔所有这些逻辑,以及应该使用哪种类型的服务? I think, it can be intent service to get the weather from provider and insert it into database, and bound service to update user layout (view). 我认为,可以从提供者那里获取天气并将其插入数据库中,然后通过绑定服务来更新用户布局(视图),这可能是一种意图服务。 Also, when we have a weather update schedule, should be created a service, that runs permanently in the background, and launches weather update process automatically. 另外,当我们有天气更新时间表时,应创建一个服务,该服务在后台永久运行,并自动启动天气更新过程。 Which type of android service schould be used in this case? 在这种情况下,应使用哪种类型的android服务?

Thank you. 谢谢。

There are several methods. 有几种方法。 Just some of them. 只是其中一些。

  1. Bind service and Activity ( imho, the ugliest) 绑定服务和活动(最丑的恕我直言)
  2. Send broadcast intent between activity and service. 在活动和服务之间发送广播意图。 Lags appear while datea transfer. 数据传输时出现延迟。
  3. Send message between of them. 在他们之间发送消息。
  4. And, imho, the most useful way (that doesn't exist is specifications) is to set both Activity and Service as interfaces (listeners) that are stored inside Application class. 而且,恕我直言,最有用的方法(尚不存在规范)是将Activity和Service都设置为存储在Application类内部的接口(侦听器)。
  5. By the way you can send data via both singleton and application class. 顺便说一下,您可以通过单例和应用程序类发送数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM