EventBus的初级使用技巧
来源:互联网 发布:在linux部署禅道 编辑:程序博客网 时间:2024/05/29 03:08
第一次写博客,不知道怎么写,也不知道写些什么,想来想去,写一些自己正在写的音乐播放器的知识吧!!!
今天写EventBus,之前我在Service和Activity之间的传值使用的是接口回调的方式,也可以运行.但是后来老师告诉我说这种方式会导致栈溢出,推荐我使用EventBus方式,代码简单,还很方便.所以我就上网查询了EventBus的使用方法,整理出来,以后想用的时候可以直接看。
1.首先我使用的开发工具是Android Studio 非常好用的开发安卓的工具.,新建一个项目叫做EventBusDemo,在Android Studio中Gradle Scripts中的build.grade(Module:app)中的dependencies里面添加一段代码:
compile 'org.greenrobot:eventbus:3.0.0'
之后重新编译一下,studio中就有EventBus的依赖了.2.然后在activity_main中添加一个button和textview:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/btn_get" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="btn_get" /> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="match_parent" android:text="1111" android:textColor="#000000" /></LinearLayout>
3.然后在新建一个SecondActivity,在其布局文件activity_second中添加一个button<Button android:id="@+id/btn_send" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Btn_Send" />
4.MainActivity.java跳转到SecondActivitypublic class MainActivity extends AppCompatActivity { Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.btn_get); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getApplicationContext(), SecondActivity.class); startActivity(intent); } }); }}
5.然后新建一个类EventInfo,这个类很简单,构造时传进去一个字符串,然后可以通过getMsg()获取出来。public class EventInfo { private String Msg; public EventInfo(String msg) { Msg = msg; } public String getMsg(){ return Msg; }}
6.我们在MainActivity中点击button跳转到SecondActivity,然后点击SecondActivity中的button将值传回MainActivity.我们是要在MainActivity中接收发过来的消息的,所以我们先在MainActivity中注册消息。public class MainActivity extends AppCompatActivity { Button button; TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //注册EventBus EventBus.getDefault().register(this); button = (Button) findViewById(R.id.btn_try); tv = (TextView) findViewById(R.id.tv); tv.setText("lll"); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getApplicationContext(), SecondActivity.class); startActivity(intent); } }); } @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this);//反注册EventBus }}
7.然后是发送消息,发送消息是使用EventBus中的Post方法来实现发送的,发送过去的是我们新建的类的实例!
SecondActivity.java中的代码为:
public class SecondActivity extends AppCompatActivity { Button btn_FirstEvent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); btn_FirstEvent = (Button) findViewById(R.id.btn_send); btn_FirstEvent.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { EventBus.getDefault().post(new EventInfo("EventInfo btn clicked")); } }); }}
8.接收消息时,我们使用EventBus中最常用的onEventMainThread()函数来接收消息,
在MainActivity中重写onEventMainThread(EventInfo event),参数就是我们自己定义的类:
在收到Event实例后,我们将其中携带的消息取出,一方面Toast出去,一方面传到TextView中;
@Subscribe public void onEventMainThread(FirstEvent event){ String msg = "onEventMainThread接收到了消息:" + event.getMsg(); Log.d("HMC", msg); tv.setText(msg); Toast.makeText(this, msg, Toast.LENGTH_LONG).show(); }完整的MainActivity.java代码如下:
public class MainActivity extends AppCompatActivity { Button button; TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //注册EventBus EventBus.getDefault().register(this); button = (Button) findViewById(R.id.btn_get); tv = (TextView) findViewById(R.id.tv); tv.setText("lll"); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getApplicationContext(), SecondActivity.class); startActivity(intent); } }); } @Subscribe public void onEventMainThread(EventInfo event){ String msg = "onEventMainThread接收到了消息:" + event.getMsg(); Log.d("HMC", msg); tv.setText(msg); Toast.makeText(this, msg, Toast.LENGTH_LONG).show(); } @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this);//反注册EventBus }}
首先注意在OnCreate中注册EventBus后下面就有OnDestory来进行反注册EventBus.我刚才运行Demo程序一直崩,后来看Error后在
onEventMainThread
上面添加了@Subscribe
就好了....
- EventBus的初级使用技巧
- EventBus的初级使用
- EventBus的初级使用
- EventBus的初级使用介绍
- EventBus 使用心得及技巧
- HTML5的初级技巧
- EventBus 《二》 Android EventBus的简单使用
- EventBus的使用初试
- EventBus的使用
- EventBus的使用
- EventBus的简单使用
- EventBus的使用
- eventbus的使用
- EventBus的初步使用:
- EventBus的使用
- EventBus的简单使用
- EventBus的使用总结
- EventBus的使用
- Cornerstone “An error occurred and the operation could not be completed”
- 关于IBM bluemix的使用
- 《2015互联网安全年报》,移动端成重灾区,黑灰产日益成熟
- Struts2学习(一) 基础环境搭建与创建HelloWorld程序
- Android Sensor Framework
- EventBus的初级使用技巧
- checkio-Xs and Os Referee
- [LeetCode]105 根据先序遍历和中序遍历构建二叉树
- Android setSelection 没起作用的解决办法
- sql 对应 sequelize
- Andorid-XML和JAVA代码混合控制UI
- 微信支付 商户API密钥key的生成与设置
- 如何在eclipse中看到java基础类的源代码
- 归并排序算法及分析