Android EventBus的初步使用

本文介绍了Android中EventBus的使用,作为替代Intent、Handler和Broadcast的工具,EventBus简化了组件间的消息传递。通过实例展示了在MainActivity、SecondActivity和ThridActivity中如何注册、解绑及发送、接收事件,强调了解耦和代码优雅性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

EventBus是一款针对Android优化的发布/订阅事件总线。主要功能是替代Intent,Handler,BroadCast在Fragment,Activity,Service,线程之间传递消息.优点是开销小,代码更优雅。以及将发送者和接收者解耦。

尝试写了一个小Demo初步使用EventBus。直接上代码:

在 MainActivity 类当中:

package com.szkingdom.android.phone.eventbusdemo;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
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 Button mBtn;
    private TextView mTv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //注册

        EventBus.getDefault().register(this);//订阅

        mBtn = (Button) findViewById(R.id.btn);
        mTv = (TextView) findViewById(R.id.tv);
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               startActivity(new Intent( MainActivity.this , SecondActivity.class));
            }
        });
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 解绑
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void onDataSynEvent( MyEventBus event) {
        mTv.setText( event.getMessage());
        Toast.makeText( this , event.getMessage() , Toast.LENGTH_SHORT).show();
    }
}

SecondActiviyt 类:

package com.szkingdom.android.phone.eventbusdemo;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
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 SecondActivity extends AppCompatActivity {
private Button mBtn;
    private TextView mTv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        // 注册
        EventBus.getDefault().register(this);

        mBtn = (Button) findViewById(R.id.secondBtn);
        mTv = (TextView) findViewById(R.id.secondTv);
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity( new Intent( SecondActivity.this , ThridActivity.class));
            }
        });
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();

        EventBus.getDefault().unregister(this);
    }
    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void onDataSynEvent(SecondEventBus event) {
        mTv.setText( event.getMessage());
        Toast.makeText( this , event.getMessage() , Toast.LENGTH_SHORT).show();
    }
}

ThridActiity 类:

package com.szkingdom.android.phone.eventbusdemo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by KDS on 2017/2/16.
 */
public class ThridActivity extends AppCompatActivity {

    private Button mBtn;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);

        mBtn = (Button) findViewById(R.id.btn);
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                System.out.println( "send the date over...");
                EventBus.getDefault().post( new MyEventBus( "this is the big date ... money 11111111111111111"));

                EventBus.getDefault().post( new SecondEventBus( "this is the big date ... money 2222222222222222222"));
                ThridActivity.this.finish();
            }
        });
    }
}

MyEventBus 类:

package com.szkingdom.android.phone.eventbusdemo;
/**
 * Created by KDS on 2017/2/16.
 */
public class MyEventBus {
    private String message ;
    public MyEventBus( String message ){
        this.message = message;
    }
    public String getMessage(){
        return this.message;
    }
}

SecondEventBus类和MyEventBus类类似。

注意在使用的时候需要添加依赖库: compile 'org.greenrobot:eventbus:3.0.0' 才可以是用 EventBus。

并且方法名onDataSynEvent可以随意命名,只要在方法上面添加相关注解就可以了。

可参看博客地址:http://www.cnblogs.com/whoislcj/p/5595714.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值