在没用eventBus之前一直用Android广播方式通知消息更新UI

广播写法

首先发送广播通知

Intent intent = new Intent();
intent.setAction("action.refreshFriend"); //名称自定义标识是哪个通知消息
sendBroadcast(intent);

接收广播通知

首先注册广播

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("action.refreshFriend");
registerReceiver(mRefreshBroadcastReceiver, intentFilter);

private BroadcastReceiver mRefreshBroadcastReceiver = new BroadcastReceiver() {

@Override 
public void onReceive(Context context, Intent intent) { 
String action = intent.getAction(); 
if (action.equals("action.refreshFriend")) 

  //更新UI


};

protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mRefreshBroadcastReceiver); //销毁广播
}

----------------------------------------------------------------

https://github.com/greenrobot/EventBus

EventBus是Android的发布/订阅事件总线优化。

首先添加引用

compile 'org.greenrobot:eventbus:3.0.0'

MainActivity代码
package com.freexiaoyu.enevtbus;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView; import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode; import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick; public class MainActivity extends AppCompatActivity {
@BindView(R.id.tv_test)
TextView tv_text;
@BindView(R.id.btn_post)
Button btn_post;
@BindView(R.id.btn_post2)
Button btn_post2;
@BindView(R.id.btn_post3)
Button btn_post3; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
EventBus.getDefault().register(this);
} @Subscribe(threadMode = ThreadMode.MAIN)
public void helloEventBus(Event event) {
switch (event.getType()){
case 1:
tv_text.setText(event.getMessage().toString());
break;
case 2:
tv_text.setText(event.getMessage().toString());
break;
case 3:
tv_text.setText(event.getMessage().toString());
break;
} } @Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
} @OnClick({R.id.btn_post,R.id.btn_post2,R.id.btn_post3})
public void submit(View view) {
switch (view.getId()){
case R.id.btn_post:
EventBus.getDefault().post(new Event(1,"我是老大"));
break;
case R.id.btn_post2:
EventBus.getDefault().post(new Event(2,"我是老二"));
break;
case R.id.btn_post3:
Intent intent=new Intent(MainActivity.this,TestActivity.class);
startActivity(intent);
break;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical"> <TextView
android:id="@id/tv_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" /> <Button
android:id="@id/btn_post"
android:layout_width="match_parent"
android:layout_height="52.0dp"
android:text="EventBus" />
<Button android:id="@id/btn_post2"
android:layout_width="match_parent"
android:layout_height="52.0dp"
android:text="EventBus" />
<Button android:id="@id/btn_post3"
android:layout_width="match_parent"
android:layout_height="52.0dp"
android:text="跳转界面" />
</LinearLayout>

TestActivity代码

package com.freexiaoyu.enevtbus;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; import org.greenrobot.eventbus.EventBus; import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick; public class TestActivity extends AppCompatActivity {
@BindView(R.id.btn_post)
Button btn_post; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
ButterKnife.bind(this);
} @OnClick({R.id.btn_post})
public void submit(View view) {
switch (view.getId()){
case R.id.btn_post:
EventBus.getDefault().post(new Event(3,"我是小三哈哈!"));
break;
}
} @Override
protected void onDestroy() {
super.onDestroy();
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"> <Button
android:id="@id/btn_post"
android:layout_width="match_parent"
android:layout_height="52.0dp"
android:text="我要更新上一页内容" />
</RelativeLayout>

Event代码

public class Event {
private int type;
private Object message; public Event(int type, Object message){
this.type=type;
this.message=message;
} public int getType() {
return type;
} public void setType(int type) {
this.type = type;
} public Object getMessage() {
return message;
}
public void setMessage(Object message) {
this.message = message;
}
}

  

DEMO地址 https://yunpan.cn/cB3SH2Ig7dAZU  访问密码 aad8

EventBus 3.0使用的更多相关文章

  1. 【热门技术】EventBus 3.0,让事件订阅更简单,从此告别组件消息传递烦恼~

    一.写在前面 还在为时间接收而烦恼吗?还在为各种组件间的消息传递烦恼吗?EventBus 3.0,专注于android的发布.订阅事件总线,让各组件间的消息传递更简单!完美替代Intent,Handl ...

  2. Android消息传递之EventBus 3.0使用详解

    前言: 前面两篇不仅学习了子线程与UI主线程之间的通信方式,也学习了如何实现组件之间通信,基于前面的知识我们今天来分析一下EventBus是如何管理事件总线的,EventBus到底是不是最佳方案?学习 ...

  3. Android EventBus 3.0.0 使用总结

    转载请标明出处:http://www.cnblogs.com/zhaoyanjun/p/6039221.html 本文出自[赵彦军的博客] 前言 EventBus框架 EventBus是一个通用的叫法 ...

  4. Android EventBus 3.0 实例使用详解

    EventBus的使用和原理在网上有很多的博客了,其中泓洋大哥和启舰写的非常非常棒,我也是跟着他们的博客学会的EventBus,因为是第一次接触并使用EventBus,所以我写的更多是如何使用,源码解 ...

  5. EventBus 3.0使用详解

    01 前言 当我们进行项目开发的时候,往往是需要应用程序的各组件.组件与后台线程间进行通信,比如在子线程中进行请求数据,当数据请求完毕后通过Handler或者是广播通知UI,而两个Fragment之家 ...

  6. EventBus 3.0使用相关

    一 引入方法 可以去github的官网中下载EventBus的相关资源  地址:https://github.com/greenrobot/EventBus 当然还有他的官方网站 http://gre ...

  7. EventBus 3.0源码解析

    现在网上讲解EventBus的文章大多数都是针对2.x版本的,比较老旧,本篇文章希望可以给大家在新版本上面带来帮助. EventBus 是专门为Android设计的用于订阅,发布总线的库,用到这个库的 ...

  8. 【转】EventBus 3.0使用详解

    原文:https://www.jianshu.com/p/f9ae5691e1bb 01 前言 当我们进行项目开发的时候,往往是需要应用程序的各组件.组件与后台线程间进行通信,比如在子线程中进行请求数 ...

  9. Android 框架学习2:源码分析 EventBus 3.0 如何实现事件总线

    Go beyond yourself rather than beyond others. 上篇文章 深入理解 EventBus 3.0 之使用篇 我们了解了 EventBus 的特性以及如何使用,这 ...

随机推荐

  1. 【转】APNs消息推送完整讲解

    https://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificat ...

  2. Remoting首次用时偏长问题

    先说我遇到的问题,我需要访问多个服务器的一个相同的Remoting方法,根据方法返回的结果决定优先使用某个服务器. var _remoteFacade = Activator.GetObject(ty ...

  3. 响应式Web设计(Responsive Web design)的理念

    页面的设计与开发应当根据用户行为以及设备环境(系统平台.屏幕尺寸.屏幕定向等)进行相应的响应和调整.具体的实践方式由多方面组成,包括弹性网格和布局.图片.CSS media query的使用等.无论用 ...

  4. iOS开发零基础--Swift篇:逻辑分支

    一. 分支的介绍 分支即if/switch/三目运算符等判断语句 通过分支语句可以控制程序的执行流程 二. if分支语句 和OC中if语句有一定的区别 判断句可以不加() 在Swift的判断句中必须有 ...

  5. bzoj 1064

    题意:戳这里 思路:很明显是一个图论模型.. 就两种图形: 1.图中存在环,那么就是所有环的gcd为最大答案.gcd的大于3的最小约数为最小答案 2.不存在环,那么是每个弱连通块的最长链之和为最大答案 ...

  6. Swing Note

                                      2. Swing容器:   内容窗格.分层窗格.玻璃窗格和一个可选的菜单条.(这四个同时包含在根窗格里)(请分别向其中添加组件)   ...

  7. linux-7 man 命令

    man  命令的分类 man 命令 代码 代表内容 普通命令 内核调用的函数与工具 常见的函数与函数库 设备文件的说明 配置文件 游戏 惯例与协议 管理员可使用的命令 内核相关的文件 一般来讲帮助文档 ...

  8. SymmetricDS 3.5.0 发布,数据同步和复制

    SymmetricDS 3.5.0 关闭 53 个问题,新增对 SQLite on Android.Sybase ASE 和 Sybase ASA 的支持:增加了文件同步功能,可同步目录.文件过滤和脚 ...

  9. Print2flash在.NET(C#)中的使用,即文档在线预览

    office文档(word,excel,ppt)在线预览查看,有很多种方式,比如可以 1.调用weboffice组件,进行word预览,要求客户端安装word,仅适用IE, word2013, IE1 ...

  10. Dynamic CRM 2013学习笔记(五)禁止修改、删除审批通过后的单据

    审批通过后的单据,一般要对其进行控制,不能修改,不能添加,删除等,下面分别介绍下如何实现: 一. 禁止修改: 1. 主表控制,如果页面上审批状态为审批中或审批通过,就把整个页面都disable掉 1: ...