EventBus 3.0使用

时间:2022-12-02 12:12:51

在没用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