Coder Social home page Coder Social logo

messagebus's Introduction

MessageBus



MessageBus能在任何地方发送消息,在任何地方接受消息。

使用说明

在接受消息的地方必须注册MessageBus,不需要接受消息的时候反注册,否则会导致内存泄漏。

代码示例

需要接收消息的地方注册MessageBus。

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        MessageBus.getBus().register(this);
        ...
    }

不需要接收消息的时候反注册MessageBus。

    @Override
    protected void onDestroy() {
        MessageBus.getBus().unregister(this);
        super.onDestroy();
    }

在任意地方发送消息

  MessageBus.getBus().send(Test test);//Object can be a class that carries message

根据参数类型接收消息

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        MessageBus.getBus().register(this);
        ...
    }
    
    //ThreadMode用来设置此方法运行的线程
    @Subscribe(ThreadMode.MAINTHREAD)
    public void recived(Test test) {  
        String id = test.getId();
        String name = test.getUsername();
    }

    @Override
    protected void onDestroy() {
        MessageBus.getBus().unregister(this);
        super.onDestroy();
    }

快速引入项目

合并以下代码到需要使用Module的dependencies中

	dependencies {
	  ...
          compile 'com.hacknife:messagebus:1.0.8'
	}




感谢浏览

如果你有任何疑问,请加入QQ群,我将竭诚为你解答。欢迎Star和Fork本仓库,当然也欢迎你关注我。
Image Text

messagebus's People

Contributors

iwdael avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Forkers

dystudio

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.