Android的回调监听 - 从POJO SDK中值发送到应用程序的活动 [英] Android callback listener - send value from pojo in SDK to an application's activity

查看:119
本文介绍了Android的回调监听 - 从POJO SDK中值发送到应用程序的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在执行一个动作,并返回一个布尔值的SDK深埋的Java类。 它没有应用程序的主要活动的知识,但我需要的主要活动以接收布尔值。

I have a java class buried deep in an SDK that performs an action and returns a boolean. It has no knowledge of the application's main activity, but I need the main activity to receive that boolean value.

我看到有关回调,广播节目和听众有很多问题,但他们似乎都具有活性的知识。我的POJO确实有一个activityContext,但我不知道如何来获取值返回给应用程序的主要活动。

I've seen a lot of questions regarding callbacks, broadcasts, and listeners but they all seem to have knowledge of the activity. My pojo does have an activityContext but I don't know how to get the value back to the application's main activity.

我已经用我的POJO的AsyncTask的,我试图找出如何发送的布尔在onPostExecute方法的方式,应用程序的主活动可以接受它。

I'm already using an AsyncTask in my pojo and I'm trying to figure out how to send the boolean in the onPostExecute method in a way that the application's main activity can receive it.

是否有人知道如何做到这一点?

Does anybody know how to do this?

推荐答案

我建议使用一个消息总线或可观察/观察者模式即可。

广场有奥托的实现消息总线一个不错的开源库。

Square has Otto a nice little open-source library that implements a message bus.

Observer模式在维基很好的描述,例如

Observer pattern is well described at wikipedia for example.

无论哪种方式,你将不得不这样做基本上是开始,如果你把它观测听或者您的POJO,或认购 onResume总线事件()(或在onStart()),并停止听的onPause()您的活动。

Either way what you will have to do is essentially start listening to either your POJO if you make it Observable, or subscribe for bus events in onResume() (or onStart()) and stop listening in onPause() in your activity.

我喜欢总线更因为它的松散耦合和事实,你可以发送任意的POJO的公共汽车和只听一种特定类型的例子。

I like bus more because of it's loose coupling and the fact that you can send any arbitrary POJOs to the bus and only listen to one specific type for example.

所以你发布一条消息是:

so you post a message this:

bus.post(new SomethingICareAbout("I really really do"));

和在你的codeBase的(在活动中您的情况)其他:

and elsewhere in your codebase (in your case in the activity):

@Subscribe
public void onSomethingIcareAbout(SomethingICareAbout thingsAndStuff) {
    // TODO: React to the event somehow. Use what you received.
}

@Subscribe
public void onSomethingElseIcareAbout(SomethingElseICareAbout otherThings) {
    // TODO: React to the event somehow. Use what you received.
}

以上是有意简化,你仍然需要创建总线并订阅它,但你会发现,在该文档中:P
此外,它使用注解,是很轻量级(codewise)。

The above is intentionally simplified, you still need to create the bus and subscribe to it, but you will find that in the docs :P Also it uses annotations and is really lightweight (codewise).

观察者/可观察的另一只就是Java的一部分,所以它是内置的。但它是紧密结合,你的活动必须实施观察,你的POJO将实施观测,并且您预订购必须实现update()方法中你的活动,这一次会得到所有的更新,无论你在观测送什么。

Observer/Observable on the other had is part of Java, so it's built in. But it is tightly coupled, your activity will have to implement Observer, your POJO will implement Observable and you willl have to implement update() method in your Activity, this one will get all the updates no matter what you send by the Observable.

我希望这是有道理的一点:)

这篇关于Android的回调监听 - 从POJO SDK中值发送到应用程序的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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