启动服务并返回数据 [英] starting a service and returning data

查看:126
本文介绍了启动服务并返回数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我学习意图和绑定服务的活动,我觉得这是很容易的和伟大的方式从服务传输数据到活动中。我想测试一下,将启动一个服务的活动,这种服务将呼吁在网页上得到一些数据。我绑定的活动到服务,使活动能够呼吁服务方法,然后举杯结果。在服务方法我称之为网页上,并返回数据。当我测试我得到一个空指针异常,即时通讯pretty确保我没有做正确的方式,没有任何人有任何建议,如何正确地实现这一点。

today I was learning about Intents and binding an activity to service, I found it quite easy and great way to transfer data from the service to the activity. I wanted to test out an activity that would start a service, this service would call on a webpage to get some data. I binded the activity to the service so that the activity could call on the service method and then toast the results. In the service method I call on a webpage and return the data. When I tested it I was getting a null pointer exception, im pretty sure I am not doing it the right way , does anyone have any suggestions how to implement this properly.

推荐答案

您正在处理一些这里相当雄厚的线程同步问题。你会更好的服务注册某种监听模式,使得您注册活动作为侦听到您的服务正在产生的数据。由于目前维持,有您的服务与活动之间没有同步可言,导致活动努力,他们已经准备好之前获取来自服务的结果。有在那里讨论监听器模式或观察者模式。

You're dealing with some fairly solid thread synchronization issues here. You would be far better served registering some kind of listener paradigm such that you register your Activity as a listener to the data that your Service is producing. As it currently stands, there's no synchronization between your Service and Activity at all, resulting in the Activity trying to fetch the results from the Service before they're ready. There are a ton of resources out there discussing the listener pattern or Observer pattern.

我要指出,我不知道你的服务是否真正拉下来的东西正确。我只是在寻找两者之间您的沟通。

I should note, I have no idea whether your Service is actually pulling things down correctly. I'm just looking at your communication between the two.

这篇关于启动服务并返回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆