1.先写一个事件
一个参数的
package com.dimi.app.evenbus;
/**
* Created by WangChunHao on 2022/02/11 09:00
* <p>
* 我的视频更新广播
*
* 接收位置
* {@link com.dimi.app.ui.activity.MyMovieActivity#onMovieEvent(MyMovieEvent)}
*/
public class MyMovieEvent {
public static final int ACTION_THUMB = 1;
private final int action;
public MyMovieEvent(int action) {
this.action = action;
}
public int getAction() {
return action;
}
}
两个参数的
package com.dimi.app.evenbus;
/**
* Created by ZhongXiaolong on 2022/01/29 14:45.
* <p>
* 我的资料更新广播
*
* 接收位置
* {@link com.dimi.app.ui.fragment.PersonFragment#onMyProfileUpdateEvent(MyProfileUpdateEvent)}
*/
public class MyProfileUpdateEvent {
public static final int ACTION_NAME = 1;
public static final int ACTION_AVATAR = 2;
private final int action;
public MyProfileUpdateEvent(int action) {
this.action = action;
}
public int getAction() {
return action;
}
}
2.使用方法
发布
/**
* 发布请求
*/
private void releaseRequest(String thumb) {
int isPrivate = mCheckBoxPrivate.isChecked() ? 1 : 0;//判断是否有点击
int coin = CastUtil.parseInt(mTvPrice.getText().toString().trim());//获取控件上的数据然后强转
String title = mTvTitle.getText().toString().trim();//获取标题控件上的数据
OkHttp.create(this).postVideo(title, String.valueOf(thumb), String.valueOf(thumb), String.valueOf(thumb),
isPrivate, coin).enqueue((call, httpRes) -> {
ToastUtil.out(httpRes.getMsg());
mProgressDialog.dismiss();
if (httpRes.isSuccessful()) {
EventBus.getDefault().post(new MyMovieEvent(MyMovieEvent.ACTION_THUMB));//发布出去
finish();
}
});
}
订阅
在要接收的activity进行接收
EventBus.getDefault().register(this);
/**
* EventBus加载视频
*/
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMovieEvent(MyMovieEvent event) {
if (event.getAction() == MyMovieEvent.ACTION_THUMB) {
mRefreshLayout.autoRefresh();//如果是这个事件,那就在这个activity加载数据
}
}
@Override
public void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);//销毁
}