EventBus黏性事件

2024-02-24 06:59
文章标签 事件 黏性 eventbus

本文主要是介绍EventBus黏性事件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

除了之前讲的普通事件外,EventBus还支持发送黏性事件。何为黏性事件呢?简单讲,就是在发送事件之后再订阅该事件也能收到该事件,跟黏性广播类似。

本程序为EventBus的黏性事件



代码框架

效果演示



实现步骤

关于EventBus的关联
见http://blog.csdn.net/dubuwucool/article/details/53523318


接收窗体布局
<?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_ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="由主页面跳转到发送事件的页面"
        android:textSize="30sp"/>
    <TextView
        android:id="@+id/txtShow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="展示EventBus发送过来的数据"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/btn_ok"/>
</RelativeLayout>

发送窗体布局
<?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"
    >

    <EditText
        android:id="@+id/edt_Show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:lines="5"
        />

    <Button
        android:id="@+id/btn_Send"
        android:layout_below="@+id/edt_Show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="向主界面发送一个eventBus事件"
        android:textSize="30sp"/>
</RelativeLayout>

黏性消息类
public class EventBusStickMessage {
    public  String Message;
    public  EventBusStickMessage(String message){
        Message =message;
    }
}

发送窗体代码
package com.dubuwucool.eventbus2;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;

public class SendActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_Send;
    private EditText edt_Show;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send);
        //初始化控件
        initView();
    }

    private void initView() {
        btn_Send = (Button) findViewById(R.id.btn_Send);

        btn_Send.setOnClickListener(this);
        edt_Show = (EditText) findViewById(R.id.edt_Show);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_Send:
                //获得输入的值
                String content = edt_Show.getText().toString();
                //通过黏性事件将值传到MainActivity窗体中
                EventBus.getDefault().postSticky(new EventBusStickMessage(content));
                Intent intent = new Intent(SendActivity.this, MainActivity.class);
                startActivity(intent);
                break;
        }
    }
}


接收窗体代码

package com.dubuwucool.eventbus2;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_ok;
    private TextView txtShow;
    boolean isTrue true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
        initView();
    }

    private void initView() {
        btn_ok = (Button) findViewById(R.id.btn_ok);
        txtShow = (TextView) findViewById(R.id.txtShow);

        btn_ok.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_ok:
                //加一个标识,进行判断,让用户不会因多次点击按钮而注册,从而导致崩溃
               if (isTrue) {
                    //你一旦注册eventBus就会接收消息
                    EventBus.getDefault().register(this);
                    isTrue false;
                }
                break;
        }
    }

    //接收消息  添加注解 如果是黏性事件 将stick设置为true
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void receiveMessage(EventBusStickMessage eventBusStickMessage) {
        txtShow.setText(eventBusStickMessage.Message);
    }
//在onDestory()方法中取消订阅:防止内存溢出
    @Override
    protected void onDestroy() {
        //移除所有黏性事件
        EventBus.getDefault().removeAllStickyEvents();
        //销毁EventBus
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}


由此便完成了EventBus黏性事件使用


这篇关于EventBus黏性事件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/741313

相关文章

禁止平板,iPad长按弹出默认菜单事件

通过监控按下抬起时间差来禁止弹出事件,把以下代码写在要禁止的页面的页面加载事件里面即可     var date;document.addEventListener('touchstart', event => {date = new Date().getTime();});document.addEventListener('touchend', event => {if (new

FreeRTOS内部机制学习03(事件组内部机制)

文章目录 事件组使用的场景事件组的核心以及Set事件API做的事情事件组的特殊之处事件组为什么不关闭中断xEventGroupSetBitsFromISR内部是怎么做的? 事件组使用的场景 学校组织秋游,组长在等待: 张三:我到了 李四:我到了 王五:我到了 组长说:好,大家都到齐了,出发! 秋游回来第二天就要提交一篇心得报告,组长在焦急等待:张三、李四、王五谁先写好就交谁的

【经验交流】修复系统事件查看器启动不能时出现的4201错误

方法1,取得『%SystemRoot%\LogFiles』文件夹和『%SystemRoot%\System32\wbem』文件夹的权限(包括这两个文件夹的所有子文件夹的权限),简单点说,就是使你当前的帐户拥有这两个文件夹以及它们的子文件夹的绝对控制权限。这是最简单的方法,不少老外说,这样一弄,倒是解决了问题。不过对我的系统,没用; 方法2,以不带网络的安全模式启动,运行命令行,输入“ne

BT天堂网站挂马事件后续:“大灰狼”远控木马分析及幕后真凶调查

9月初安全团队披露bt天堂网站挂马事件,该网站被利用IE神洞CVE-2014-6332挂马,如果用户没有打补丁或开启安全软件防护,电脑会自动下载执行大灰狼远控木马程序。 鉴于bt天堂电影下载网站访问量巨大,此次挂马事件受害者甚众,安全团队专门针对该木马进行严密监控,并对其幕后真凶进行了深入调查。 一、“大灰狼”的伪装 以下是10月30日一天内大灰狼远控的木马样本截图,可以看到该木马变种数量不

react笔记 8-19 事件对象、获取dom元素、双向绑定

1、事件对象event 通过事件的event对象获取它的dom元素 run=(event)=>{event.target.style="background:yellowgreen" //event的父级为他本身event.target.getAttribute("aid") //这样便获取到了它的自定义属性aid}render() {return (<div><h2>{

react笔记 8-18 事件 方法 定义方法 获取/改变数据 传值

1、定义方法并绑定 class News extends React.Component {constructor(props) {super(props)this.state = {msg:'home组件'}}run(){alert("我是一个run") //方法写在类中}render() {return (<div><h2>{this.state.msg}</h2><button onCli

【Qt】定时器事件

定时器事件 在之前学习QTimer中实现了定时器的功能,而在QTimer背后是QTimerEvent定时器事件进行支撑的。在QObject中提供了一个timeEvent这个函数。 startTimer启动定时器killTimer关闭定时器 Qt 中在进⾏窗⼝程序的处理过程中,经常要周期性的执⾏某些操作,或者制作⼀些动画效果,使⽤定 时器就可以实现。所谓定时器就是在间隔⼀定时间后,去执⾏某⼀

Imageview在百度地图中实现点击事件

1.首先第一步,需要声明的全局有关类的引用 private BMapManager mBMapMan; private MapView mMapView; private MapController mMapController; private RadioGroup radiogroup; private RadioButton normalview; private RadioBu

Winform中在窗体中的Paint事件中重绘会导致递归问题?

在 WinForms 应用程序中,如果在窗体的 Paint 事件处理程序中不断调用 Invalidate 方法,确实可能会导致递归调用的问题。这是因为每次调用 Invalidate 方法时,都会向消息队列添加一个绘制消息,当消息队列中的绘制消息被处理时,会触发 Paint 事件。如果 Paint 事件处理程序中又调用了 Invalidate,就会形成一个循环,导致递归调用 Paint 事件,这

GIS圈大事件!Cesium被收购了,是好是坏?

大家好,我是日拱一卒的攻城师不浪,致力于技术与艺术的融合。这是2024年输出的第34/100篇文章。 Cesium开发交流群+V:brown_7778(备注来意) 一觉醒来,突然看到Cesium官方发的消息,宣布通过收购的方式加入Bentley软件公司。 可能小伙伴们对Bentley公司还不是很了解。 Bentley 是数字孪生领域的长期合作伙伴,也是开放生态系统的真正支持者。