当前位置: 代码迷 >> 综合 >> EventBus 事件学习和学习
  详细解决方案

EventBus 事件学习和学习

热度:25   发布时间:2023-12-14 02:24:09.0
 1.对于粘滞事件,产生后一直存在,可以反复接收,EventBus.getDefault().registerSticky会触发接收之前已经产生的粘滞事件,并且执行事件函数
若EventBus.getDefault().registerSticky执行时并没有可以接收的粘滞事件,不会执行事件函数的哦

2.onEventMainThread(SecondEvent event) {

注意这里的的参数类型决定了EventBus.getDefault().post出来的事件是否会通知这个事件函数(当时就执行哦)



package com.example.administrator.studyeventbus01;import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;import de.greenrobot.event.EventBus;/*** 我向MainActivity里面注册事件可以理解为向EventBus表示我是事件的观察者* 我向MainActivity里面反注册事件可以理解为向EeentBus表示我不再是事件的观察者*/
public class MainActivity extends Activity {Button btn;TextView tv;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//注册EventBus//  EventBus.getDefault().register(this);EventBus.getDefault().registerSticky(this);btn = (Button) findViewById(R.id.btn_try);tv = (TextView)findViewById(R.id.tv);btn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(getApplicationContext(), SecondActivity.class);startActivity(intent);}});}@Overrideprotected void onDestroy(){super.onDestroy();EventBus.getDefault().unregister(this);//反注册EventBus}public void onEvent(SecondEvent event) {String msg = "onEventMainThread收到了消息:" + event.getMsg();Log.d("harvic", msg);tv.setText(msg);Toast.makeText(this, msg, Toast.LENGTH_LONG).show();}/*** onEventMainThread是本我这个事件观察者接受事件消息并且只在UI线程执行的** 通过EventBus.getDefault().post(new FirstEvent("FirstEvent btn clicked"));方法调用之后* 可以及时调用到onEventMainThread,不需要等等回到这个页面才调用哦* 参考地址:http://blog.csdn.net/harvic880925/article/details/40660137* @param event*/public void onEventMainThread(SecondEvent event) {String msg = "onEventMainThread收到了消息:" + event.getMsg();Log.d("harvic", msg);tv.setText(msg);Toast.makeText(this, msg, Toast.LENGTH_LONG).show();}/*public void onEventBackground(FirstEvent event) {String msg = "onEventMainThread收到了消息:" + event.getMsg();Log.d("harvic", msg);tv.setText(msg);Toast.makeText(this, msg, Toast.LENGTH_LONG).show();}public void onEventAsync(FirstEvent event) {String msg = "onEventMainThread收到了消息:" + event.getMsg();Log.d("harvic", msg);tv.setText(msg);Toast.makeText(this, msg, Toast.LENGTH_LONG).show();}*/
}




 
 
package com.example.administrator.studyeventbus01;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;import de.greenrobot.event.EventBus;public class SecondActivity extends Activity {private Button btn_FirstEvent;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_second);btn_FirstEvent = (Button) findViewById(R.id.btn_first_event);btn_FirstEvent.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//     EventBus.getDefault().post(new FirstEvent("FirstEvent btn clicked"));EventBus.getDefault().postSticky(new FirstEvent("FirstEvent btn clicked"));}});btn_FirstEvent.setOnLongClickListener(new View.OnLongClickListener() {@Overridepublic boolean onLongClick(View v) {Intent intent = new Intent(getApplicationContext(), ThirdActivity.class);startActivity(intent);return false;}});}
}

package com.example.administrator.studyeventbus01;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;import de.greenrobot.event.EventBus;/*** 我向MainActivity里面注册事件可以理解为向EventBus表示我是事件的观察者* 我向MainActivity里面反注册事件可以理解为向EeentBus表示我不再是事件的观察者*/
public class ThirdActivity extends Activity {Button btn;TextView tv;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//注册EventBus//  EventBus.getDefault().register(this);//      EventBus.getDefault().registerSticky(this);<strong><span style="color:#FF0000;">//这个位置不行,这里执行后直接执行那个事件方法时,控件还没有初始化呢</span></strong>btn = (Button) findViewById(R.id.btn_try);tv = (TextView)findViewById(R.id.tv);btn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(getApplicationContext(), SecondActivity.class);startActivity(intent);}});EventBus.getDefault().registerSticky(this);  /<strong><span style="color:#FF0000;">/现在的位置是合适的,粘滞事件可以反复接收到,但是如果执行这句话的时候没有事件通知,则不执行事件函数</span></strong>}@Overrideprotected void onDestroy(){super.onDestroy();EventBus.getDefault().unregister(this);//反注册EventBus}/*   public void onEvent(FirstEvent event) {String msg = "onEventMainThread收到了消息11:" + event.getMsg();Log.d("harvic", msg);tv.setText(msg);btn.setText(msg);Toast.makeText(this, msg, Toast.LENGTH_LONG).show();}*/public void onEventMainThread(FirstEvent event) {String msg = "onEventMainThread收到了消息111:" + event.getMsg();Log.d("harvic", msg);String aa = btn.getText().toString();btn.setText("123456");Toast.makeText(this, msg, Toast.LENGTH_LONG).show();}/*public void onEventBackground(FirstEvent event) {String msg = "onEventMainThread收到了消息:" + event.getMsg();Log.d("harvic", msg);tv.setText(msg);Toast.makeText(this, msg, Toast.LENGTH_LONG).show();}public void onEventAsync(FirstEvent event) {String msg = "onEventMainThread收到了消息:" + event.getMsg();Log.d("harvic", msg);tv.setText(msg);Toast.makeText(this, msg, Toast.LENGTH_LONG).show();}*/
}

参考:http://blog.csdn.net/harvic880925/article/details/40787203(分上下两篇博客)