EventBus 3.0使用
在没用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使用的更多相关文章
- 【热门技术】EventBus 3.0,让事件订阅更简单,从此告别组件消息传递烦恼~
一.写在前面 还在为时间接收而烦恼吗?还在为各种组件间的消息传递烦恼吗?EventBus 3.0,专注于android的发布.订阅事件总线,让各组件间的消息传递更简单!完美替代Intent,Handl ...
- Android消息传递之EventBus 3.0使用详解
前言: 前面两篇不仅学习了子线程与UI主线程之间的通信方式,也学习了如何实现组件之间通信,基于前面的知识我们今天来分析一下EventBus是如何管理事件总线的,EventBus到底是不是最佳方案?学习 ...
- Android EventBus 3.0.0 使用总结
转载请标明出处:http://www.cnblogs.com/zhaoyanjun/p/6039221.html 本文出自[赵彦军的博客] 前言 EventBus框架 EventBus是一个通用的叫法 ...
- Android EventBus 3.0 实例使用详解
EventBus的使用和原理在网上有很多的博客了,其中泓洋大哥和启舰写的非常非常棒,我也是跟着他们的博客学会的EventBus,因为是第一次接触并使用EventBus,所以我写的更多是如何使用,源码解 ...
- EventBus 3.0使用详解
01 前言 当我们进行项目开发的时候,往往是需要应用程序的各组件.组件与后台线程间进行通信,比如在子线程中进行请求数据,当数据请求完毕后通过Handler或者是广播通知UI,而两个Fragment之家 ...
- EventBus 3.0使用相关
一 引入方法 可以去github的官网中下载EventBus的相关资源 地址:https://github.com/greenrobot/EventBus 当然还有他的官方网站 http://gre ...
- EventBus 3.0源码解析
现在网上讲解EventBus的文章大多数都是针对2.x版本的,比较老旧,本篇文章希望可以给大家在新版本上面带来帮助. EventBus 是专门为Android设计的用于订阅,发布总线的库,用到这个库的 ...
- 【转】EventBus 3.0使用详解
原文:https://www.jianshu.com/p/f9ae5691e1bb 01 前言 当我们进行项目开发的时候,往往是需要应用程序的各组件.组件与后台线程间进行通信,比如在子线程中进行请求数 ...
- Android 框架学习2:源码分析 EventBus 3.0 如何实现事件总线
Go beyond yourself rather than beyond others. 上篇文章 深入理解 EventBus 3.0 之使用篇 我们了解了 EventBus 的特性以及如何使用,这 ...
随机推荐
- 一个叫 team 的表,里面只有一个字段name, 一共有4 条纪录,分别是a,b,c,d, 对应四个球队,现在四个球队进行比赛,用一条sql 语句显示所有可能的比赛组合.
select *from timp a, timp b where a.name > b.name 结果:
- Microsoft Visual Studio 2010 VSTS单元测试指南
本来以为很简单的一个问题,今天预计10分钟搞定,结果到下班还没弄出结果,单元测试运行的时候一直处于无反应状态,最后估计可能是我装的2010有问题,结果到家一试果然是有问题,有时软件就是这么神奇. 言归 ...
- linux环境变量查看及修改
例如用命令 echo $PATH 则可以查看该环境变量为/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin 添加环境定义一个变量 ...
- Windows 8.1 应用再出发 - 视图状态的更新
本篇我们来了解一下Windows 8.1 给应用的视图状态带来了哪些变化,以及我们怎么利用这些变化作出更好的界面视图. 首先我们来简单回顾一下Windows 8.0 时代的视图状态: 上图中, ...
- [转]moveTaskToback退后台
http://blog.csdn.net/dacainiao007/article/details/17352367 方法:public boolean moveTaskToBack(boolean ...
- C#之delegate
delegate 委托的使用: 封装一个方法,该方法只有一个参数并且不返回值. using System; using System.Windows.Forms; delegate void Disp ...
- c#中的static
1.C# 不支持静态局部变量(在方法范围内声明的变量). 2.static类一般用于与状态无关的类.那么,什么是与状态无关的类?我的理解是当一个类中没有属性,只有方法的的时候,就可以认为这个类是与状态 ...
- 敏捷开发 与 Scrum
敏捷开发以用户的需求进化为核心,采用迭代.循序渐进的方法进行软件开发.在敏捷开发中,软件项目在构建初期被切分成多个子项目,各个子项目的成果都经过测试,具备可视.可集成和可运行使用的特征.换言之,就是把 ...
- StarUML建模软件
这星期本人进行了UML建模语言的初步学习,简单地将上学期所建立的数据库模型在该软件中实现了出来.
- Redis使用总结之与Memcached异同
Redis是什么?两句话可以做下概括: 1. 是一个完全开源免费的key-value内存数据库 2. 通常被认为是一个数据结构服务器,主要是因为其有着丰富的数据结构 strings.map. list ...