2011-02-06 176 views
3

我是新來的android/java,目前正在嘗試學習自定義事件和偵聽器。我目前已經在下面實現了這個代碼,它正在成功運行。我現在想向DataRobot添加另一個事件。使用analyzeData函數分析某個數據段時,會觸發該事件。我想知道是否有人能告訴我如何在這個課程中實施兩個活動?我正在使用此事件來觸發AlarmDialog。所以,如果有更好的方法,讓我知道。java事件和監聽器,一個類中的多個事件

/* SmartApp.java */ 
public class SmartApp extends Activity 
{ 
    private ConnectDevice cD = new ConnectDevice(); 
    private DataRobot dR = new DataRobot(); 
    private DataBuilder dB = new DataBuilder(); 
    private DataSender dS = new DataSender(); 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.intro); 

    cD.addDataReceivedListener(new DataReceivedListener() { 
     @Override 
     public void dataReceivedReceived(DataReceivedEvent event) { 
      // TODO Auto-generated method stub 
      dR.analyzeData(event.getData()); 
     } 
    }); 
    dR.addDataAnalyzedListener(new DataAnalyzedListener() { 
     @Override 
     public void dataAnalyzedReceived(DataAnalyzedEvent event) { 
      // TODO Auto-generated method stub 
      dB.submitData(event.getData()); 
     } 
    }); 
    dB.addDataBuilderListener(new DataBuilderListener() { 
     @Override 
     public void dataBuilderReceived(DataBuilderEvent event) { 
      // TODO Auto-generated method stub 
      dS.sendData(event.getData()); 
     } 
    }); 
     } 
} 

/* DataRobot.java */ 
public class DataRobot { 
    /* This class is for analyzing the data */ 
    private List _listeners = new ArrayList(); 
    private String data; 
    public boolean analyzeData(String temp) { 
     /* Analyze the data 
     * This function analyzes the data, as explained in the OP 
     * This function fires the analyzed data event when finished 
      * analyzing the data. 
     */ 
     data = temp; 
     fireDataAnalyzedEvent(data); // this fires the dataanalyzedevent 
     return true; //for now this will always return true 
    } 

    public synchronized void addDataAnalyzedListener(DataAnalyzedListener listener) { 
     _listeners.add(listener); 
    } 
    public synchronized void removeDataAnalyzedListener(DataAnalyzedListener listener) { 
     _listeners.remove(listener); 
    } 
    private synchronized void fireDataAnalyzedEvent(String temp) { 
     DataAnalyzedEvent dRE = new DataAnalyzedEvent(this, temp); 
     Iterator listeners = _listeners.iterator(); 
     while(listeners.hasNext()) { 
      ((DataAnalyzedListener)listeners.next()).dataAnalyzedReceived(dRE); 
     } 
    } 
    public interface DataAnalyzedListener { 
     public void dataAnalyzedReceived(DataAnalyzedEvent event); 
    } 
} 

/* DataReceivedEvent.java */ 
public class DataReceivedEvent extends EventObject{ 
    private String data; 
    public DataReceivedEvent(Object source, String temp) { 
     super(source); 
     // TODO Auto-generated constructor stub 
     data = temp; 
    } 
    public String getData() { 
      // this function is just an accessor function 
     return data; 
    } 
} 

/* DataAnalyzedEvent.java */ 
public class DataAnalyzedEvent extends EventObject{ 
    private String data; 
    public DataAnalyzedEvent(Object source, String temp) { 
     super(source); 
     // TODO Auto-generated constructor stub 
     data = temp; 
    } 
    public String getData() { 
      // this function is just an accessor function 
     return data; 
    } 
} 

回答

2

如何添加,如果兩個事件有關的第二事件取決於。如果一個物體想要聽這兩個事件,那麼就像向你的DataAnalyzedListener接口添加另一種方法一樣簡單。

另一種情況是事件不相關,對象將要訂閱一個事件或另一個事件。在這種情況下,您將需要創建兩個獨立的接口並擁有兩個偵聽器列表。

其他意見:

使用參數化的列表,而不是裸露的類型,你會不會需要轉換的對象。 也可以使用foreach循環,而不是直接抓取迭代器。

private List<DataAnalyzedListener> _listeners = new ArrayList<DataAnalyzedListener>(); 

for (DataAnalyzedListener listener : _listeners) { 
    listener.dataAnalyzedReceived(dRE); 
} 
+0

感謝您的建議。現在會嘗試。 – prolink007 2011-02-06 03:00:13

3

我會重新設計Android。使用服務並註冊它以接收具有某些操作類型的Intents。以這種方式來看待:意圖就是你的事件。意圖過濾器是你的意圖映射,事件管理器是通過調用context.startActivity(Intent)或者通過廣播Intent(pub/sub eventing)這就是Android的方式。