• EventBus通信


    需求:

    1.ActivityA打开ActivityB

    2.在B中执行某操作后,同时执行A中的方法

    lib下载:eventbus-2.4.0.jar  jmmy

    1.在EventBusTestActivity注册eventBus

    EventBus.getDefault().register(this);// 注册广播

    2.点击事件跳转

    Intent intent = new Intent(getApplicationContext(),EventBusTest2Activity.class);
    startActivity(intent);

    3.在EventBusTest2Activity中发送广播

    EventBus.getDefault().post(new MyEvent("Message From EventBusTest2"));

    4.EventBusTestActivity接收广播

    // onEvent会放在发送事件的那个线程中去执行,不能进行UI更新
        public void onEvent(MyEvent event) {
            Log.i("What", "[onEvent]My Thread is "
                    + Thread.currentThread().getName());
        }
    
    // onEventMainThread就是会放到主线程去执行的事件处理,一般在其中进行比如UI的更新的操作
        public void onEventMainThread(MyEvent event) {
            Log.i("What", "[onEventMainThread] Thread is "
                    + Thread.currentThread().getName());
            test1.setText("测试完成");
        }

    5.解除广播

    protected void onDestroy() {
      super.onDestroy();
      EventBus.getDefault().unregister(this);// 解除
    }

    下面贴上源码:

    package com.example.activity;
    
    import com.example.event.MyEvent;
    
    import de.greenrobot.event.EventBus;
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    public class EventBusTestActivity extends Activity implements OnClickListener {
        private Button test1;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_event_bus_test);
            test1 = (Button) findViewById(R.id.test1);
            test1.setOnClickListener(this);
            EventBus.getDefault().register(this);// 注册广播
        }
    
        // onEvent会放在发送事件的那个线程中去执行,不能进行UI更新
        public void onEvent(MyEvent event) {
            Log.i("What", "[onEvent]My Thread is "
                    + Thread.currentThread().getName());
        }
    
        // onEventMainThread就是会放到主线程去执行的事件处理,一般在其中进行比如UI的更新的操作
        public void onEventMainThread(MyEvent event) {
            Log.i("What", "[onEventMainThread] Thread is "
                    + Thread.currentThread().getName());
            test1.setText("测试完成");
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.test1:
                Intent intent = new Intent(getApplicationContext(),
                        EventBusTest2Activity.class);
                startActivity(intent);
                break;
            }
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            EventBus.getDefault().unregister(this);// 解除
        }
    }
    package com.example.activity;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    import com.example.event.MyEvent;
    
    import de.greenrobot.event.EventBus;
    
    public class EventBusTest2Activity extends Activity {
        private Button click1;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_event_bus_test2);
    
            click1 = (Button) findViewById(R.id.click1);
            click1.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    EventBus.getDefault().post(
                            new MyEvent("Message From EventBusTest2"));
                }
            });
        }
    }
  • 相关阅读:
    【转】return 使用示例
    java基础_二维数组的行和列
    新版SQL授权用户时报错 near 'IDENTIFIED BY '密码' with grant option' at line 1
    GO kafka sarama 生产者 消费者 简单 实现
    Windows 安装kafka
    windows 连接nsq
    reflect: call of reflect.Value.NumField on ptr Value
    django 数据库 mysql 事务 处理
    python 类的继承
    python 中 insert 返回 None
  • 原文地址:https://www.cnblogs.com/stayreal/p/5088317.html
Copyright © 2020-2023  润新知