我是android / java的新手,目前正在尝试学习自定义事件和监听器。我目前在下面实现了这个成功运行的代码。我现在想向DataRobot添加另一个事件。使用analyzeData函数分析某个数据时,将触发该事件。我想知道是否有人可以告诉我如何在这一个类中实现两个事件?我正在使用此事件来触发AlarmDialog。所以,如果有更好的方法,请告诉我。
/* 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;
}
}
答案 0 :(得分:3)
我会在Android中重新设计。使用服务并注册它以接收具有某些操作类型的Intent。以这种方式看待它:意图是你的事件。 Intent Filter是你的Intent映射,事件管理器是通过调用context.startActivity(Intent)或通过广播Android方式的Intent(pub / sub eventing)来实现的。
答案 1 :(得分:2)
如何添加第二个事件取决于两个事件是否相关。如果一个对象想要监听这两个事件,那么就像在DataAnalyzedListener
接口中添加另一个方法一样简单。
另一种情况是事件不相关,对象想要订阅一个事件或另一个事件。在这种情况下,您将需要创建两个单独的接口并具有两个侦听器列表。
其他评论:
使用参数化列表而不是裸类型,您不需要转换对象。 你也可以使用foreach循环而不是直接抓取迭代器。
private List<DataAnalyzedListener> _listeners = new ArrayList<DataAnalyzedListener>();
for (DataAnalyzedListener listener : _listeners) {
listener.dataAnalyzedReceived(dRE);
}