zoukankan      html  css  js  c++  java
  • 传递消息--第三方开源--EventBus的简单使用

     EventBus下载地址:https://github.com/greenrobot/EventBus 

    MyEvent:

     1 package com.zzw.testeventbus;
     2 
     3 public class MyEvent {
     4     public int id;
     5     public String content;
     6     
     7     public MyEvent() {
     8         super();
     9         
    10     }
    11 
    12     @Override
    13     public String toString() {
    14         return "MyEvent [id=" + id + ", content=" + content + "]";
    15     }
    16     
    17 }
    MyEvent

    MainActivity:

     1 package com.zzw.testeventbus;
     2 
     3 import android.app.Activity;
     4 import android.content.Intent;
     5 import android.graphics.Color;
     6 import android.os.Bundle;
     7 import android.util.Log;
     8 import android.view.View;
     9 import android.view.View.OnClickListener;
    10 import android.widget.TextView;
    11 import de.greenrobot.event.EventBus;
    12 
    13 public class MainActivity extends Activity {
    14     TextView tv;
    15     public static final String TAG = "MainActivity";
    16 
    17     @Override
    18     protected void onCreate(Bundle savedInstanceState) {
    19         super.onCreate(savedInstanceState);
    20         setContentView(R.layout.activity_main);
    21         tv = (TextView) findViewById(R.id.tv);
    22 
    23         EventBus.getDefault().register(this);
    24 
    25         findViewById(R.id.bt).setOnClickListener(new OnClickListener() {
    26             @Override
    27             public void onClick(View v) {
    28                 Intent intent = new Intent(MainActivity.this, EventService.class);
    29                 startService(intent);
    30             }
    31         });
    32     }
    33     // Main线程,这个与Android UI线程密切相关,可以在里面设置UI值,不要阻塞它!
    34     public void onEventMainThread(MyEvent event) {
    35         Log.d(TAG + "--" + "onEventMainThread", event.toString());
    36         tv.setText(event.content);
    37         int id = event.id % 3;
    38         if (id == 0) {
    39             tv.setTextColor(Color.RED);
    40         } else if (id == 1) {
    41             tv.setTextColor(Color.GREEN);
    42         } else if (id == 2) {
    43             tv.setTextColor(Color.BLUE);
    44         }
    45     }
    46 
    47     // 后台线程中接收处理
    48     public void onEventBackgroundThread(MyEvent event) {
    49         Log.e(TAG + "--" + "onEventBackgroundThread", event.toString());
    50     }
    51 
    52     // 异步线程中接收处理
    53     public void onEventAsync(MyEvent event) {
    54         Log.i(TAG + "--" + "onEventAsync", event.toString());
    55     }
    56 
    57     @Override
    58     protected void onDestroy() {
    59         EventBus.getDefault().unregister(this);
    60     }
    61 }

    EventService:

     1 package com.zzw.testeventbus;
     2 
     3 import android.app.Service;
     4 import android.content.Intent;
     5 import android.os.IBinder;
     6 import android.os.SystemClock;
     7 import de.greenrobot.event.EventBus;
     8 
     9 public class EventService extends Service {
    10 
    11     @Override
    12     public void onCreate() {
    13 
    14         super.onCreate();
    15     }
    16 
    17     private void task() {
    18         new Thread(new Runnable() {
    19 
    20             @Override
    21             public void run() {
    22                 for (int i = 1; i <= 100; i++) {
    23                     MyEvent event = new MyEvent();
    24                     event.id = i;
    25                     event.content = "傻逼" + i;
    26                     EventBus.getDefault().post(event);
    27                     SystemClock.sleep(1000);
    28                 }
    29             }
    30         }).start();
    31     }
    32 
    33     @Override
    34     public int onStartCommand(Intent intent, int flags, int startId) {
    35         task();
    36         return super.onStartCommand(intent, flags, startId);
    37     }
    38 
    39     @Override
    40     public void onDestroy() {
    41         super.onDestroy();
    42     }
    43 
    44     @Override
    45     public IBinder onBind(Intent intent) {
    46         return null;
    47     }
    48 
    49 }
  • 相关阅读:
    KDrive 與 Embedded Linux
    windows内存管理初探
    开源方案
    boot time 优化
    psplash
    linux下操纵大于2G文件
    【技术贴】Windows图片和传真查看器打开图片慢,正在生成预览的解决办法!
    【转】c++.primer.plus.第五版.中文版[下载]
    【技术贴】魂斗罗坦克Normal Tanks第五关以及第5、6、7、关的LICENCE CODE的查
    【转】奇文共欣赏,疑义相与析:原文转载《电脑维护技巧》(N条举措N条理由)并请大家交流研讨
  • 原文地址:https://www.cnblogs.com/zzw1994/p/4953655.html
Copyright © 2011-2022 走看看