如何实现事件和Android的授权概念?如何实现、概念、事件、Android

2023-09-05 09:51:22 作者:来自你心的我

我有一个调用Web服务并执行XML解析一个活动。我希望我的活动等待XML解析类来执行,然后我想我的acitvity继续。我想知道如果有一个事件委托的概念,是present Android中通过,我可以让我的XML解析类回应我的活动时,它已经结束了。

I have a activity which calls a webservice and does xml parsing. i want my activity to wait for the xml parsing class to execute and then i want my acitvity to continue. i was wondering if there is an event delegate concept that is present in android by which i can make my xml parsing class respond to my activity when it is over.

推荐答案

是的,有。你一定会喜欢的 ResultReceiver 类。要创建一个你将需要通过处理程序(活动内部创建),并覆盖 onReceiveResult 方法。

Yes, there's. You will love the ResultReceiver class. To create one you will need to pass a Handler (created inside the activity), and override the onReceiveResult method.

所以,你要做的就是发送 ResultReceiver 的参考服务(使用意图演员) ,当XML分析完成后,您拨打的服务发送方法。这样,你的行为将被通知的XML解析完成。

So, what you do is sending a reference of the ResultReceiver to the service (using the Intent extras), and when the XML parsing is done, you call the send method from the Service. That way your activity will be notified that the XML parsing has finished.

有一个谷歌IO视频,其中该技术解释。您还可以下载的幻灯片在会议中使用。

There's a Google IO video where this technique is explained. You can also download the slides used in the conference.

如果你想例如code,看看在 iosched 的应用程序。它会教如何创建一个 ResultReceiver 的代理,这将有助于你处理的配置变化(如设备轮换变化)......,因为你知道,当这种情况发生的用户界面重新创建,从而可能会导致内存泄漏(你知道,该服务将指向不存在的UI元素)。

If you want example code, take a look at the iosched app. It will teach how to create a ResultReceiver proxy that will help you deal with configuration changes (e.g. the device rotation changes)... because, as you know, when that happens the UI is recreated, thus it could cause memory leaks (you know, the service will be pointing to non-existing UI elements).