Android(安卓)EventBus使用,粘性事件postSticky使用
16lz
2022-01-16
源码下载:
https://download.csdn.net/download/qq_31939617/10508747 下载
1.在Android studio中新建一个项目,配置build.gradle, 添加compile ‘org.greenrobot:eventbus:3.0.0’,然后Sync Now,下载eventbus需要的文件;**
2.注册,取消eventbus
// 注册EventBus
EventBus.getDefault().register(this);
//取消EventBus
EventBus.getDefault().unregister(this);
3.开始编写events,所谓的Events其实就是一个普通的Java对象;
package com.example.sz.eventbustest;public class FristEvent { private String type; public String getType() { return type; } public void setType(String type) { this.type = type; } public String getStr() { return str; } public void setStr(String str) { this.str = str; } private String str ; public void FristEvent( String type, String str){ this.type = type; this.str = str; }}
MainActivity.class
package com.example.sz.eventbustest;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.TextView;import org.greenrobot.eventbus.EventBus;public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv = findViewById(R.id.tv); tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //发送消息, //一般用 post发送, @Subscribe接收就可以了 // 此处因为发送消息的时候,SecondActivity还没启动,所以必须用粘性事件postSticky发送 // 所以接收的时候必须用粘性事件接收@Subscribe(threadMode= ThreadMode.MAIN,sticky=true) String type = "s"; EventBus.getDefault().postSticky(new FristEvent(type,"Hello")); startActivity(new Intent(MainActivity.this,SecondActivity.class)); } }); }}
SecondActivity .class
package com.example.sz.eventbustest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
public class SecondActivity extends AppCompatActivity {
private static final String TAG = “SecondActivity”;
TextView tv;
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); tv = findViewById(R.id.tv); // 注册EventBus EventBus.getDefault().register(this);}@Overrideprotected void onDestroy() { super.onDestroy(); //取消EventBus EventBus.getDefault().unregister(this);}//发送消息,//一般用 post发送, @Subscribe接收就可以了// 此处因为发送消息的时候,SecondActivity还没启动,所以必须用粘性事件postSticky发送// 所以接收的时候必须用粘性事件接收@Subscribe(threadMode= ThreadMode.MAIN,sticky=true)@Subscribe(threadMode= ThreadMode.MAIN,sticky=true)public void onEvent(FristEvent event) { String type = event.getType(); if (type.equals("s")) { String str = event.getStr(); tv.setText(str); }}
}
“`
源码下载:
https://download.csdn.net/download/qq_31939617/10508747 下载
更多相关文章
- Android: 打印Bundle内容
- Android(安卓)activity 参数传递
- Android: 打印Bundle内容
- 【Android】Android(安卓)发送短信和打电话的方法
- android之发送短信的方法研究
- Android开发傻瓜入门-开发自己的手机短信发送程序
- Android--Socket通信
- Android(安卓)APK文件安装过程小结
- Android上监听收到的SMS