gpt4 book ai didi

android - 奥托事件收到两次

转载 作者:行者123 更新时间:2023-11-29 15:56:38 26 4
gpt4 key购买 nike

我尝试使用 Otto .我认为它到目前为止有效,但我收到了两次事件。

这就是我所拥有的(我遵循了 example on GitHub ):

制作人:

public class NetworkChangeReceiver extends BroadcastReceiver
{
@Override
public void onReceive(final Context context, final Intent intent)
{
Log.i("ralphs", "onReceive");

// .... more stuff

final Bus bus = TRaumfeldApp.getInstance().getBus();
bus.register(this);
bus.post(new NetworkChangeEvent());
bus.unregister(this);
}

@Produce
public NetworkChangeEvent produceAnswer()
{
Log.i("ralphs", "produceAnswer");
return new NetworkChangeEvent();
}
}

订阅者:

public class MainActivity extends ActionBarActivity
{
private Bus mBus;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// otto event bus
mBus = TRaumfeldApp.getInstance().getBus();
}

@Override
protected void onResume()
{
super.onResume();
mBus.register(this);
}

@Override
protected void onPause()
{
super.onPause();
mBus.unregister(this);
}

@Subscribe
public void answerAvailable(NetworkChangeEvent event)
{
Log.i("ralphs", "answerAvailable");
}
}

日志锁是这样的:

I/ralphs﹕ onReceive
I/ralphs﹕ produceAnswer
I/ralphs﹕ answerAvailable
I/ralphs﹕ answerAvailable

为什么我收到两次事件?

最佳答案

当您的订阅者注册时,如果注册了具有此类方法的对象,它会从带注释的@Produce 方法获取第一个事件。第二个事件来自显式 post() 操作。

您无需注册即可发帖,但您确实需要为 @Produce 注释注册。

关于android - 奥托事件收到两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27625997/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com