需求:

1.ActivityA打开ActivityB

2.在B中执行某操作后,同时执行A中的方法

lib下载:eventbus-2.4.0.jar  jmmy

1.在EventBusTestActivity注册eventBus

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

2.点击事件跳转

Intent intent = new Intent(getApplicationContext(),EventBusTest2Activity.class);
startActivity(intent);

3.在EventBusTest2Activity中发送广播

EventBus.getDefault().post(new MyEvent("Message From EventBusTest2"));

4.EventBusTestActivity接收广播

// onEvent会放在发送事件的那个线程中去执行,不能进行UI更新
public void onEvent(MyEvent event) {
Log.i("What", "[onEvent]My Thread is "
+ Thread.currentThread().getName());
} // onEventMainThread就是会放到主线程去执行的事件处理,一般在其中进行比如UI的更新的操作
public void onEventMainThread(MyEvent event) {
Log.i("What", "[onEventMainThread] Thread is "
+ Thread.currentThread().getName());
test1.setText("测试完成");
}

5.解除广播

protected void onDestroy() {
  super.onDestroy();
  EventBus.getDefault().unregister(this);// 解除
}

下面贴上源码:

package com.example.activity;

import com.example.event.MyEvent;

import de.greenrobot.event.EventBus;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class EventBusTestActivity extends Activity implements OnClickListener {
private Button test1; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_bus_test);
test1 = (Button) findViewById(R.id.test1);
test1.setOnClickListener(this);
EventBus.getDefault().register(this);// 注册广播
} // onEvent会放在发送事件的那个线程中去执行,不能进行UI更新
public void onEvent(MyEvent event) {
Log.i("What", "[onEvent]My Thread is "
+ Thread.currentThread().getName());
} // onEventMainThread就是会放到主线程去执行的事件处理,一般在其中进行比如UI的更新的操作
public void onEventMainThread(MyEvent event) {
Log.i("What", "[onEventMainThread] Thread is "
+ Thread.currentThread().getName());
test1.setText("测试完成");
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.test1:
Intent intent = new Intent(getApplicationContext(),
EventBusTest2Activity.class);
startActivity(intent);
break;
}
} @Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);// 解除
}
}
package com.example.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; import com.example.event.MyEvent; import de.greenrobot.event.EventBus; public class EventBusTest2Activity extends Activity {
private Button click1; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_bus_test2); click1 = (Button) findViewById(R.id.click1);
click1.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
EventBus.getDefault().post(
new MyEvent("Message From EventBusTest2"));
}
});
}
}

EventBus通信的更多相关文章

  1. EventBus通信小能手

     1.EventBus简介 EventBus 是由 greenrobot 组织开发的一个 Android 事件发布/订阅轻量级框架,特点:代码简洁,是一种发布订阅设计模式(观察者设计模式). Even ...

  2. vertx的Actor模型实现

    前言 note: Context 与 EventLoop 关系 : N ; 每次创建一个vericles或者multi instances 通过EventLoopGroup.next挑出一个Event ...

  3. 整理4种Vue组件通信方式

    整理4种Vue组件通信方式 重点是梳理了前两个,父子组件通信和eventBus通信,我觉得Vue文档里的说明还是有一些简易,我自己第一遍是没看明白. 父子组件的通信 非父子组件的eventBus通信 ...

  4. Android之EventBus1.0 和EventBus3.0的使用详解

    当Android项目越来越庞大的时候,应用的各个部件之间的通信变得越来越复杂,那么我们通常采用的就是Android中的解耦组件EventBus.EventBus是一款针对Android优化的发布/订阅 ...

  5. vue开发知识点总结

    一.vue介绍 Vue.js 是一套构建用户界面(UI)的渐进式JavaScript框架,是一个轻量级MVVM(model-view-viewModel)框架. 二.数据绑定 最常用的方式:Musta ...

  6. 快速Android开发系列通信篇之EventBus

    先吐槽一下博客园的MarkDown编辑器,推出的时候还很高兴博客园支持MarkDown了,试用了下发现支持不完善就没用了,这次这篇是在其他编辑器下写的,复制过来后发现..太烂了.怎么着作为一个技术博客 ...

  7. UWP开源项目 LLQNotifier 页面间通信利器(移植EventBus)

    前言 EventBus是一个Android版本的页面间通信库,这个库让页面间的通信变得十分容易且大幅降低了页面之间的耦合.小弟之前玩Android的时候就用得十分顺手,现在玩uwp就觉得应该在这平台也 ...

  8. EventBus完全解析--组件/线程间通信利器

    github地址:https://github.com/greenrobot/EventBus 1, Android EventBus实战, 没听过你就out了 2,  Android EventBu ...

  9. Android 通信 EventBus

    参考: Android 框架炼成 教你如何写组件间通信框架EventBus Android EventBus源码解析 带你深入理解EventBus Android EventBus实战 没听过你就ou ...

随机推荐

  1. Reverse Words in a String (JAVA)

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

  2. WinForm设置控件焦点(转)

    http://blog.csdn.net/zlwzlwzlw/article/details/8573921 winform窗口打开后文本框的默认焦点设置,进入窗口后默认聚焦到某个文本框,两种方法: ...

  3. Qt WebEngine 网页交互

    环境:Qt5.7.0,VS2013 一.简单介绍 从 Qt5.4 开始已经去掉 Qt WebKit 模块了,使用的是 chrome 内核封装的 QtWebEngine,浏览器相关的类有以下几个: QW ...

  4. LayoutInflater的获取方式

    1.LayoutInflater是什么? 通俗而讲,就是将xml中定义的布局找出来. 2.获取LayoutInflater的三种方式   1. LayoutInflater inflater = ge ...

  5. 计算两点间的距离-hdu2001

    Problem Description 输入两点坐标(X1,Y1),(X2,Y2),计算并输出两点间的距离.   Input 输入数据有多组,每组占一行,由4个实数组成,分别表示x1,y1,x2,y2 ...

  6. 【Javascript下载文件的Post实现】

    /** *从服务器上下载数据 *@param paras Json格式的键值对参数 */ downLoadFromServer: function (paras) { //init a new win ...

  7. hdu 1715 大菲波数_java

    用java的大数解决 import java.math.BigInteger; import java.util.Scanner; public class Main { public static ...

  8. PHP - Mysql数据库备份类

    使用方法: require_once("backdata.class.php"); $link =@mysql_connect("localhost",&quo ...

  9. CentOS 6.8yum源的配置

    Centos配置163的yum源 1.首先备份当前系统的yum源 # mv /etc/yum.repo.d/Centos-Base.repo /etc/yum.repo.d/Centos-Base.r ...

  10. 最少步数(dfs + bfs +bfs优化)

    最少步数 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 ...