EventBus - Android's Event Bus

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EventBus - Android's Event Bus相关的知识,希望对你有一定的参考价值。

技术分享图片

[1] About EventBus

EventBus is a publish/subscribe event bus for android

技术分享图片

EventBus...

  • simplifies the communication between components
    • decouples event senders and receivers
    • performs well with Activities, Fragments, and background threads
    • avoids complex and error-prone dependencies and life cycle issues
  • makes your code simpler
  • is fast
  • is tiny (~50k jar)
  • is proven in practice by apps with 100,000,000+ installs
  • has advanced features like delivery threads, subscriber priorities, etc.

[2] Add EventBus

In your build.gradle :

dependencies {
    compile ‘org.greenrobot:eventbus:3.0.0‘
}

[3] Use EventBus

  1. Define events:
package com.netcircle.myeventbusdemo;
/**
 * Created by sweetgirl on 2017/11/27
 */
public class UserMessage {
    public final String mName ;
    public final String mPassword;

    public UserMessage(String name, String password){
        this.mName = name;
        this.mPassword = password;
    }
}
  1. Prepare subscribers:
package com.netcircle.myeventbusdemo;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
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 textView;
    private EditText input_name;
    private EditText input_password;
    private Button btn_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        EventBus.getDefault().register(this);
        initView();

    }

    private void initView(){

        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.tv_signup);
        input_name = (EditText) findViewById(R.id.input_name);
        input_password = (EditText) findViewById(R.id.input_password);

        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
    }

    @Subscribe
    public void onMessageEvent(UserMessage event) {

     Toast.makeText(MainActivity.this,"MainActivity"+event.mName+event.mPassword,Toast.LENGTH_SHORT).show();
     Log.i("MainActivity"+event.mName,"MainActivity-psw"+event.mPassword);

    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}
  1. Post events:
package com.netcircle.myeventbusdemo;
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.Button;
import android.widget.EditText;
import org.greenrobot.eventbus.EventBus;
public class RegisterActivity extends AppCompatActivity {

    private EditText et_input_name;
    private EditText et_input_password;
    private Button btn_registered;

    private String name;
    private String psw;

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


        et_input_name = (EditText) findViewById(R.id.et_input_name);
        et_input_password = (EditText) findViewById(R.id.et_input_password);
        btn_registered = (Button) findViewById(R.id.btn_registered);


        name = et_input_name.getText().toString();
        psw = et_input_password.getText().toString();

        btn_registered.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                name = et_input_name.getText().toString();
                psw = et_input_password.getText().toString();

                Log.i("RegisterActivity-name"+name,"RegisterActivity-psw"+psw);

                EventBus.getDefault().post(new UserMessage(name,psw));

                Intent intent = new Intent(RegisterActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });
    }
}

[4] Demo Complete

see Log

11-28 10:39:05.656 25999-25999/com.netcircle.myeventbusdemo I/RegisterActivity-name123: RegisterActivity-pswqwe
11-28 10:39:05.676 25999-25999/com.netcircle.myeventbusdemo I/MainActivity123: MainActivity-pswqwe

以上是关于EventBus - Android's Event Bus的主要内容,如果未能解决你的问题,请参考以下文章

Android编程入门--开源框架EventBus

EventBus:Android EventBus实战, 没听过你就out了

Android EventBus源码解析, 带你深入理解EventBus

android eventbus有啥作用

Android之EventBus使用

Android之EventBus使用详解