网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

EventBusUsage

EventBus is a publish/subscribe event bus optimized for Android.

青县网站制作公司哪家好,找创新互联!从网页设计、网站建设、微信开发、APP开发、响应式网站等网站项目制作,到程序开发,运营维护。创新互联从2013年创立到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联。

so make it simple,just think EventBus as a framework that allow different compoents to communicate,

usually a subscribe register a certain event,then whenever a publisher has post an event,any subscribes who had register this event will be able to receive the notify.

This is an example,Let's say MainActivity is a subscribe who has register to receive RecvEvent event,

And Second Activity is a publicer who will post a RecvEvent when his btn is clicked.Theoretically speaking,MainActivity is able to recevie the event that posted by SecondActivity.

//From above,we know that EventBus contains three objects:Event,Subscribe,Publisher

//Event
public class RecvEvent
{
private final String info;
public RecvEvent(String info)
{ 
this.info = info;
}
public String getInfo()
{
return info;
}
}
//Subscribe:MainActivity
public class MainActivity extends AppCompatActivity {
   private TextView tvShowInfo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvShowInfo = (TextView)findViewById(R.id.tvShowRecv);
//register to recevie ShowRecvEvent event
        EventBus.getDefault().register(this);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
//destroy res
        EventBus.getDefault().unregister(this);
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRecvEvent(ShowRecvEvent event)
    {
        tvShowInfo.setText(event.getInfo());
    }
    public void onJump(View view)
    {
        startActivity(new Intent(this,SecondActivity.class));
    }
}
//publisher:SecondActivity
public class SecondActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);
    }
    public void onSend(View view)
    {
        EventBus.getDefault().post(new ShowRecvEvent("this is from second activity"));
    }
}

名称栏目:EventBusUsage
网页链接:http://bjjierui.cn/article/ipeisj.html

其他资讯