源码下载:

https://download.csdn.net/download/qq_31939617/10508747 下载

1.在Android studio中新建一个项目,配置build.gradle, 添加compile ‘org.greenrobot:eventbus:3.0.0’,然后Sync Now,下载eventbus需要的文件;**

2.注册,取消eventbus

// 注册EventBus
EventBus.getDefault().register(this);

//取消EventBus
EventBus.getDefault().unregister(this);

3.开始编写events,所谓的Events其实就是一个普通的Java对象;

package com.example.sz.eventbustest;public class FristEvent {    private String type;    public String getType() {        return type;    }    public void setType(String type) {        this.type = type;    }    public String getStr() {        return str;    }    public void setStr(String str) {        this.str = str;    }    private String str ;    public void  FristEvent( String type, String str){        this.type = type;        this.str = str;    }}

MainActivity.class

package com.example.sz.eventbustest;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.TextView;import org.greenrobot.eventbus.EventBus;public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        TextView tv = findViewById(R.id.tv);        tv.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                //发送消息,                //一般用 post发送, @Subscribe接收就可以了                // 此处因为发送消息的时候,SecondActivity还没启动,所以必须用粘性事件postSticky发送               // 所以接收的时候必须用粘性事件接收@Subscribe(threadMode= ThreadMode.MAIN,sticky=true)                String type = "s";                EventBus.getDefault().postSticky(new FristEvent(type,"Hello"));                startActivity(new Intent(MainActivity.this,SecondActivity.class));            }        });    }}
SecondActivity .class

package com.example.sz.eventbustest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class SecondActivity extends AppCompatActivity {
private static final String TAG = “SecondActivity”;
TextView tv;

@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_second);    tv = findViewById(R.id.tv);    // 注册EventBus    EventBus.getDefault().register(this);}@Overrideprotected void onDestroy() {    super.onDestroy();    //取消EventBus    EventBus.getDefault().unregister(this);}//发送消息,//一般用 post发送, @Subscribe接收就可以了// 此处因为发送消息的时候,SecondActivity还没启动,所以必须用粘性事件postSticky发送// 所以接收的时候必须用粘性事件接收@Subscribe(threadMode= ThreadMode.MAIN,sticky=true)@Subscribe(threadMode= ThreadMode.MAIN,sticky=true)public void onEvent(FristEvent event) {    String type = event.getType();    if (type.equals("s")) {       String str = event.getStr();        tv.setText(str);    }}

}
“`

源码下载:

https://download.csdn.net/download/qq_31939617/10508747 下载

更多相关文章

  1. Android: 打印Bundle内容
  2. Android(安卓)activity 参数传递
  3. Android: 打印Bundle内容
  4. 【Android】Android(安卓)发送短信和打电话的方法
  5. android之发送短信的方法研究
  6. Android开发傻瓜入门-开发自己的手机短信发送程序
  7. Android--Socket通信
  8. Android(安卓)APK文件安装过程小结
  9. Android上监听收到的SMS

随机推荐

  1. Android Studio如何查看获取MD5和SHA1
  2. 《Android系统源代码情景分析》连载回忆
  3. Android(安卓)对话框
  4. Android 异步消息处理机制(Handler 、 Loo
  5. Android开发者面试一百题
  6. android binder 机制三(匿名Service)
  7. 推荐给Android开发者的抢手书单
  8. android + red5 + rtmp
  9. 解决Android SDK Manager不能更新的问题
  10. Android消息机制之ThreadLocal浅析