如何使用EventBus的onEvent方法? [英] How to use the EventBus onEvent method?

查看:549
本文介绍了如何使用EventBus的onEvent方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用在我的Andr​​oid应用程序EventBus。在我mainActivity,我有发送实时数据的EventBus此处理方法如下:

 私人最终处理程序处理程序=新的处理程序(){
    @覆盖
    公共无效的handleMessage(消息MSG){
        开关(msg.what){
            案例TGDevice.MSG_STATE_CHANGE:
                。EventBus.getDefault()postSticky(msg.arg1);
                ...

我使用Fragments类,我需要从处理程序接收消息。

我已经注册了onCreateView方法片断类,如下所示:

 公开查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
                         捆绑savedInstanceState){
    //充气的布局该片段
    查看查看= inflater.inflate(R.layout.fragment_data_log,集装箱,FALSE);
    。EventBus.getDefault()寄存器(本);
    电视=(TextView中)view.findViewById(R.id.tv);
}公共无效的onEvent(消息消息){
    tv.setText(信号+消息);
}

和我有是假设时,有一个事件被称为的onEvent方法。不幸的是,这种方法不会被调用。我想这可能是方法被覆盖,但它似乎并不如此。

什么我需要做的从EventBus?

信息阅读

此外,在调试模式,在哪里可以看到我所创建的线程数? (我使用Android的一室公寓)


解决方案

  

不幸的是,这种方法不会被调用。


这是因为你的的onEvent()需要消息,以及(presumably)你是不是张贴消息。您发布的任何 ARG1


  

此外,在调试模式,在哪里可以看到我所创建的线程数? (我使用Android的一室公寓)


走进了Android调试监视器(工具> Android的>从主菜单的Andr​​oid调试监控),并有一个线程在DDMS查看里面有

I'm using EventBus in my Android application. In my mainActivity, I have this handler method which sends live data to the EventBus as follows:

private final Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case TGDevice.MSG_STATE_CHANGE:
                EventBus.getDefault().postSticky(msg.arg1);
                ...

I'm using Fragments class and I need to receive the message from the handler.

I have registered the Fragment class in the onCreateView method as follows:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_data_log, container, false);
    EventBus.getDefault().register(this);
    tv = (TextView) view.findViewById(R.id.tv);
}

public void onEvent(Message message){
    tv.setText("Signal" + message);
}

And I have the onEvent method which is suppose to be called when there is an Event. Unfortunately, this method is never called. I thought it might be method to be overridden but it doesn't seem to be.

What do i need to do to read from messages from the EventBus?

Also, in debugging modes, where can I see the number of threads being created? (I'm using Android Studio)

解决方案

Unfortunately, this method is never called

That is because your onEvent() takes a Message, and (presumably) you are not posting a Message. You are posting whatever arg1 is.

Also, in debugging modes, where can I see the number of threads being created? (I'm using Android Studio)

Go into the Android Debug Monitor (Tools > Android > Android Debug Monitor from the main menu), and there's a threads view in DDMS inside of there.

这篇关于如何使用EventBus的onEvent方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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