EventBus - Android's Event Bus

來源:互聯網
上載者:User

標籤:android   sim   ima   receive   use   login   code   har   final   

[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-pswqwe11-28 10:39:05.676 25999-25999/com.netcircle.myeventbusdemo I/MainActivity123: MainActivity-pswqwe

EventBus - Android's Event Bus

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.