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 的特性以及如何使用,这 ...
随机推荐
- php pdo预处理语句与存储过程
很多更成熟的数据库都支持预处理语句的概念.什么是预处理语句?可以把它看作是想要运行的 SQL 的一种编译过的模板,它可以使用变量参数进行定制.预处理语句可以带来两大好处: 1.查询仅需解析(或预处理) ...
- Cocos2d-x Application Wizard for Visual Studio User Guide
0. Overview Cocos2d-x-win32's project can be generated by Wizard. Wizard supports Visual Studio 2008 ...
- 20145301&20145321&20145335实验一
这次实验我的组员为:20145301赵嘉鑫.20145321曾子誉.20145335郝昊 实验内容详见:实验一报告
- JqueryEasyUI浅谈---视频教程公布
http://pan.baidu.com/s/1pJqGXez 前两天我在博客园发了一个关于JqueryEasyUI浅谈本地化应用的博客,我简单的介绍了JqueryEasyUI的应用,今天我录制了了一 ...
- ASP.NET MVC学习之控制器篇
一.前言 许久之后终于可以继续我的ASP.NET MVC连载了,之前我们全面的讲述了路由相关的知识,下面我们将开始控制器和动作的讲解. ASP.NET MVC学习之路由篇幅(1) ASP.NET MV ...
- 尝试在mac上用dotnet cli运行asp.net core示例程序
自从知道微软用dotnet cli取代dnx之后,一直在等dotnet cli支持asp.net core... 昨天看到这篇新闻(ASP.NET Core 1.0 Hello World)后,才知道 ...
- Bad version number in .class file
TY项目是用JDK1.6做的,早先在电脑上装了一个1.5的,这回就不能用了.为了能用,我就又装了一个1.6的,修改了环境变量之后,以为一切OK.开始测试,首先在Myeclipse中打开我用1.5编的一 ...
- [WinAPI] API 1 [桌面上画一个简单彩色图形]
#include<Windows.h> void GdiOut(HDC hdc); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hP ...
- [BTS] Deploy Command
BizTalkDeploymentTools.AddResource.bat @Echo OFF SET ApplicationName=%~1 SET ComponentType=%~2 SET C ...
- iis6.0报以下的错。。
Could not load file or assembly 'System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec8 ...