博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
EventBus
阅读量:4977 次
发布时间:2019-06-12

本文共 5131 字,大约阅读时间需要 17 分钟。

apply plugin: 'com.android.application'android {    compileSdkVersion 26    buildToolsVersion "27.0.1"    defaultConfig {        applicationId "com.bwie.eventbuslian"        minSdkVersion 16        targetSdkVersion 26        versionCode 1        versionName "1.0"        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"        javaCompileOptions {            annotationProcessorOptions                    {                        arguments = [eventBusIndex: 'com.bwie.butterknife.MyEventBusIndex']                    }        }    }    buildTypes {        release {            minifyEnabled false            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'        }    }}dependencies {    compile fileTree(dir: 'libs', include: ['*.jar'])    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {        exclude group: 'com.android.support', module: 'support-annotations'    })    compile 'com.android.support:appcompat-v7:26.+'    compile 'com.android.support.constraint:constraint-layout:1.0.2'    compile 'org.greenrobot:eventbus:3.1.1'    testCompile 'junit:junit:4.12'    annotationProcessor 'org.greenrobot:eventbus-annotation-processor:3.1.1'}
package com.bwie.eventbuslian;/** * Created by lenovo on 2017/11/29. */public class First {    private String Msg;    public First(String msg) {        Msg = msg;    }    public String getMsg() {        return Msg;    }}
package com.bwie.eventbuslian;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.TextView;import android.widget.Toast;import org.greenrobot.eventbus.EventBus;import org.greenrobot.eventbus.Subscribe;import org.greenrobot.eventbus.ThreadMode;public class Main2Activity extends AppCompatActivity {    private TextView tv2;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main2);        EventBus.getDefault().register(this);         tv2= (TextView) findViewById(R.id.tv_2);        findViewById(R.id.bt_2).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                startActivity(new Intent(getApplicationContext(),SecondActivity.class));            }        });    }    @Subscribe(threadMode = ThreadMode.MAIN)    public void onEventBusMainThread(First first){         String f="消息是"+first.getMsg();        tv2.setText(f);        Toast.makeText(this,f, Toast.LENGTH_SHORT).show();    }    @Override    protected void onDestroy() {        super.onDestroy();        EventBus.getDefault().unregister(this);    }}
package com.bwie.eventbuslian;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.TextView;import android.widget.Toast;import org.greenrobot.eventbus.EventBus;import org.greenrobot.eventbus.Subscribe;import org.greenrobot.eventbus.ThreadMode;public class MainActivity extends AppCompatActivity {    private TextView tv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //注册EventBus        EventBus.getDefault().register(this);         tv = (TextView) findViewById(R.id.tv);        findViewById(R.id.bt).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                startActivity(new Intent(getApplicationContext(),SecondActivity.class));            }        });        findViewById(R.id.bt_main2).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                startActivity(new Intent(MainActivity.this,Main2Activity.class));            }        });    }    @Subscribe(threadMode = ThreadMode.MAIN)public void  onEventMainThread(First first){    String msg="接受到的消息是"+first.getMsg();    Log.e("哈哈哈哈",msg);    tv.setText(msg);    Toast.makeText(this,"hahahahahahaahahaha"+msg, Toast.LENGTH_SHORT).show();}    @Override    protected void onDestroy() {        super.onDestroy();        EventBus.getDefault().unregister(this);//取消注册    }}
package com.bwie.eventbuslian;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import org.greenrobot.eventbus.EventBus;public class SecondActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_second);        Button bt= (Button) findViewById(R.id.bt_fa);        bt.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                EventBus.getDefault().post(new First("郭宇雷...shit"));            finish();            }        });    }}

 

转载于:https://www.cnblogs.com/shangliang88/p/8124288.html

你可能感兴趣的文章
selenium下打开Chrome报错解决
查看>>
HDU-1150 Machine Schedule(二分图、匈牙利)
查看>>
bzoj3156 防御准备
查看>>
Eclipse修改编码格式
查看>>
生成器和协程 —— 你想知道的都在这里了
查看>>
初级算法-6.两个数组的交集 II
查看>>
欧拉函数 / 蒙哥马利快速幂 / 容斥
查看>>
Makefile
查看>>
软件开发文档以及项目开发流程理解
查看>>
2019微软Power BI 每月功能更新系列——Power BI 4月版本功能完整解读
查看>>
truncate 、delete、drop的区别
查看>>
DynamoDB 中的限制
查看>>
mysql做主从配置
查看>>
Docker练习例子:基于 VNCServer + noVNC 构建 Docker 桌面系统
查看>>
《码出高效 Java开发手册》第六章 数据结构与集合
查看>>
软件工程-读书笔记(1-3章)
查看>>
iOS 电话在后台运行时,我的启动图片被压缩
查看>>
初学者可能不知道的vue技巧
查看>>
poj-1700 crossing river(贪心题)
查看>>
Cheese Aizu - 0558 (搜索题)
查看>>