zoukankan      html  css  js  c++  java
  • Android消息通信 第三方开源项目EventBus 的用法

    EventBus是github上的一个第三方开发库,其在github上的项目主页地址:https://github.com/greenrobot/EventBus
    EventBus的消息模型是消息发布者/订阅者机制。

     (1)EventBus是消息发布者(发送消息)/订阅者(接收消息)模式。EventBus的消息发布十分灵活,可以在工程代码中的任意位置发送消息,EventBus 发布消息只需要一行代码即可实现: EventBus.getDefault().post(event); Event即为自己定义的类的实例。

    (2)EventBus在接收消息的Activity(或Fragment)中初始化。通常在Android的Activity(或者Fragment)的onCreate里面注册,仅需一行代码: EventBus.getDefault().register(this); 类似于注册一个消息监听Listener,完了不要忘记注销EventBus,在onDestory里面 EventBus.getDefault().unregister(this);

    (3)EventBus接收消息。 在Activity中根据代码实际情况写一个EventBus的消息接收函数: public void onEventMainThread(MyEvent event); 然后,只要EventBus发送消息,就可以在这里接收到。

      EventBus的消息回调接收消息函数还有几个: onEventMainThread:Main线程,这个与Android UI线程密切相关,不要阻塞它! onEventBackgroundThread:故名思议,后台线程中接收处理。 onEventAsync:异步线程中接收处理。

     1 package com.lixu.chuandi;
     2 
     3 import android.app.Activity;
     4 import android.content.Intent;
     5 import android.os.Bundle;
     6 import android.view.View;
     7 import android.view.View.OnClickListener;
     8 import android.widget.Button;
     9 import android.widget.TextView;
    10 import de.greenrobot.event.EventBus;
    11 
    12 public class MainActivity extends Activity {
    13     TextView tv;
    14 
    15     @Override
    16     protected void onCreate(Bundle savedInstanceState) {
    17         super.onCreate(savedInstanceState);
    18         setContentView(R.layout.activity_main);
    19         tv = (TextView) findViewById(R.id.tv1);
    20         Button button = (Button) findViewById(R.id.btn1);
    21         EventBus.getDefault().register(this);
    22         button.setOnClickListener(new OnClickListener() {
    23 
    24             @Override
    25             public void onClick(View v) {
    26                 Event event = new Event();
    27                 event.a = "你好啊 我是:";
    28                 event.b = "小新";
    29 
    30                 EventBus.getDefault().post(event);
    31                 Intent intent = new Intent(MainActivity.this, MyAppService.class);
    32                 startService(intent);
    33 
    34             }
    35         });
    36 
    37     }
    38 
    39     // 这里,EventBus回调接受消息,然后更新Main UI的TextView
    40     public void onEventMainThread(Event event) {
    41 
    42         tv.setText(event.toString());
    43     }
    44 
    45     @Override
    46     protected void onDestroy() {
    47 
    48         super.onDestroy();
    49         EventBus.getDefault().unregister(this);
    50         Intent intent = new Intent(MainActivity.this, MyAppService.class);
    51         stopService(intent);
    52     }
    53 }
     1 package com.lixu.chuandi;
     2 
     3 import android.app.Service;
     4 import android.content.Intent;
     5 import android.os.IBinder;
     6 import android.util.Log;
     7 import de.greenrobot.event.EventBus;
     8 
     9 public class MyAppService extends Service {
    10     @Override
    11     public void onCreate() {
    12         super.onCreate();
    13         EventBus.getDefault().register(this);
    14     }
    15 
    16     @Override
    17     public int onStartCommand(Intent intent, int flags, int startId) {
    18         Event event = new Event();
    19         // 可以随意在工程中的任意位置发送消息给接受者
    20         EventBus.getDefault().post(event.toString());
    21         return super.onStartCommand(intent, flags, startId);
    22     }
    23 
    24     @Override
    25     public IBinder onBind(Intent intent) {
    26         return null;
    27     }
    28 
    29     // 在后台中接收消息
    30     public void onEventBackgroundThread(Event event) {
    31 
    32         Log.e("MyAppService收到消息:", event.toString());
    33     }
    34 }
     1 package com.lixu.chuandi;
     2 
     3 public class Event {
     4     public String a;
     5     public String b;
     6     @Override
     7     public String toString() {
     8         return a+b;
     9     }
    10     
    11 }
  • 相关阅读:
    RxJava学习经验谈——map与flatmap
    不要打断链式结构:使用 RxJava的 compose() 操作符
    Android Retrofit网络请求Service,@Path、@Query、@QueryMap、@Map...
    Android 勤用RXJava compose操作符消除重复代码
    Android使用token维持登陆状态的方法
    Java服务器对外提供接口以及Android端向服务器请求数据
    Retrofit请求数据对错误以及网络异常的处理
    Retrofit2.0+OkHttp打印Request URL(请求地址参数)
    HUE配置文件hue.ini 的hbase模块详解(图文详解)(分HA集群和非HA集群)
    安装Hue后的一些功能的问题解决干货总结(博主推荐)
  • 原文地址:https://www.cnblogs.com/labixiaoxin/p/4953733.html
Copyright © 2011-2022 走看看