zoukankan      html  css  js  c++  java
  • 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"));
                }
            });
        }
    }
  • 相关阅读:
    常规渗透:没遇到过的anquan狗
    SQLi-db 批量注入工具+教程
    国外整理的一套在线渗透测试资源合集
    重大漏洞!PHP multipart/form-data头部解析远程拒绝服务漏洞
    MS15-051 修正版Exploit(Webshell可用)
    网站渗透常用到的Python小脚本
    项目<<魔兽登录系统>>
    第七章:存储过程
    第六章:事务,视图和索引
    SQL高级查询:嵌套和分页
  • 原文地址:https://www.cnblogs.com/stayreal/p/5088317.html
Copyright © 2011-2022 走看看