EventBusUsage-创新互联

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

创新互联-云计算及IDC服务提供商,涵盖公有云、IDC机房租用、四川电信机房托管、等保安全、私有云建设等企业级互联网基础服务,联系电话:028-86922220

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"));
    }
}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享标题:EventBusUsage-创新互联
文章路径:http://bzwzjz.com/article/gippc.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 网站制作报价 网站建设开发 成都网站建设 响应式网站设计 成都网站制作 成都商城网站制作 教育网站设计方案 网站建设公司 成都做网站建设公司 网站建设方案 商城网站建设 自适应网站设计 网站建设方案 成都网站建设公司 成都网站制作 高端网站设计推广 定制级高端网站建设 手机网站建设 成都网站设计 古蔺网站建设 外贸网站建设 网站制作