EventBus是最近項目用到的,也只是會些簡單的功能,不過感覺功能蠻強大的。代碼鏈接:http://download.csdn.net/detail/qq_29774291/9629346 EventBus是Guava的事件處理機制,是設計模式中的觀察者模式(生產/消費者編程模型)的優雅實現。主要 ...
EventBus是最近項目用到的,也只是會些簡單的功能,不過感覺功能蠻強大的。代碼鏈接:http://download.csdn.net/detail/qq_29774291/9629346
EventBus是Guava的事件處理機制,是設計模式中的觀察者模式(生產/消費者編程模型)的優雅實現。主要功能是替代Intent,Handler,BroadCast在Fragment,Activity,Service,線程之間傳遞消息.優點是開銷小,代碼更優雅。以及將發送者和接收者解耦。
一.下載的類庫:https://github.com/greenrobot/EventBus
二.基本的使用:
1.在要接收消息的頁面註冊:
1 EventBus.getDefault().register(this);
2.在發送界面發送消息:
Student student = new Student(20, "張三", "男"); EventBus.getDefault().post(student);
3.在接受界面接受信息:
@Subscribe public void onMessageReviced(final Student Message) { //顯示一個彈窗 System.out.println("收到消息dddx :" + Message); if(Message != null){ showAlertDialog(this, "姓名:" + Message.getName() + "\\年齡:" + Message.getAge() + "\\性別:" +Message .getSex()); } }
4.解除註冊:
@Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); EventBus.getDefault().unregister(this); }
主程式代碼
1 package com.item.jiejie.event; 2 3 import de.greenrobot.event.EventBus; 4 import de.greenrobot.event.Subscribe; 5 import android.os.Bundle; 6 import android.app.Activity; 7 import android.app.AlertDialog; 8 import android.content.Context; 9 import android.content.DialogInterface; 10 import android.content.Intent; 11 import android.view.View; 12 import android.widget.Button; 13 import android.widget.TextView; 14 15 public class MainActivity extends Activity { 16 17 private TextView tv_View; 18 private Button btn_to_finish; 19 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.activity_main); 24 setView(); 25 EventBus.getDefault().register(this); 26 } 27 28 @Override 29 protected void onDestroy() { 30 // TODO Auto-generated method stub 31 super.onDestroy(); 32 EventBus.getDefault().unregister(this); 33 } 34 private void setView() { 35 // TODO Auto-generated method stub 36 tv_View = (TextView) findViewById(R.id.tv_text); 37 btn_to_finish = (Button) findViewById(R.id.btn_to_one); 38 btn_to_finish.setOnClickListener(new View.OnClickListener() { 39 40 @Override 41 public void onClick(View arg0) { 42 // TODO Auto-generated method stub 43 startActivity(new Intent(MainActivity.this, OneActivity.class)); 44 } 45 }); 46 } 47 private boolean isShow = false; 48 @Override 49 protected void onStart() { 50 // TODO Auto-generated method stub 51 super.onStart(); 52 isShow = true; 53 } 54 @Override 55 protected void onStop() { 56 // TODO Auto-generated method stub 57 super.onStop(); 58 isShow =false; 59 } 60 @Subscribe 61 public void onMessageReviced(final String Message) { 62 tv_View.setText(Message); 63 System.out.println("收到消息ddd :" + Message); 64 } 65 66 @Subscribe 67 public void onMessageReviced(final Student Message) { 68 //顯示一個彈窗 69 System.out.println("收到消息dddx :" + Message); 70 if(Message != null){ 71 showAlertDialog(this, "姓名:" + Message.getName() + "\\年齡:" + Message.getAge() + "\\性別:" +Message 72 .getSex()); 73 } 74 } 75 private void showAlertDialog(final Context context,String string){ 76 AlertDialog.Builder dialog = new AlertDialog.Builder(context); 77 dialog.setTitle("我是標題"); 78 dialog.setMessage(string); 79 dialog.setPositiveButton("確定", new DialogInterface.OnClickListener() { 80 81 @Override 82 public void onClick(DialogInterface arg0, int arg1) { 83 // TODO Auto-generated method stub 84 85 } 86 }); 87 AlertDialog mDialog = dialog.create(); 88 mDialog.show(); 89 } 90 }