从队列更新活动的最佳方法 [英] Best way to update Activity from a Queue
问题描述
在我的"Producer-Mediator-Consumer"模型中,调解器中有一个LinkedBlockingQueue
.生产者首先更新添加到activityQueue的介体.接下来,消费者/活动在队列中等待/收听,并获取下一个项目.
I have a LinkedBlockingQueue
in a Mediator in my "Producer-Mediator-Consumer" model. Producer first updates the Mediator adding to the activityQueue. Next the Consumer/Activity waits/listens on the queue and grabs the next item.
我希望活动查看队列大小已更改,然后获取下一个项目. 调解员对活动没有可见性,只有活动可以看到调解员.那么如何创建我想要的侦听器机制?
I want an Activity to see the queue size has changed and grab the next item. The Mediator has no visibility into the activity only the activity can see the Mediator. So how do I create this listener mechanism i want?
这是我的调解器类,其中包含队列,并且活动"将以某种方式查看队列并通知其是否需要更新.进入队列的数据有时可能是杂乱无章的,因此轮询机制将无法正常工作.
Here is my mediator class that holds the Queue and the Activity will somehow look at the queue and get informed if it needs to update. Data coming into the queue can be spotty and random at times so a polling mechanism will not work.
public class MediatorData {
/** Queue for the Activity */
LinkedBlockingQueue <byte[]> queueConsumer = new LinkedBlockingQueue <byte[]>();
/**
* Add data to a queue(s) for consumption
*/
public void put(byte[] data) throws InterruptedException {
queueConsumer.add(data);
}
/**
* Return data from the queue for the Feature calculations
*/
public byte[] getFeatureData() throws InterruptedException {
return queueConsumer.poll(100, TimeUnit.MILLISECONDS);
}
}
我的活动类的示例,它是一个图形类,因此队列侦听器必须高效且快速.
Example of my activity class, its a graphing class so the queue listener has to be efficient and fast.
public class DisplayGraph extends Activity {
// populated from Application Class where its created
pirvate MediatorData md;
public void onCreate(Bundle savedInstanceState) {
md = getMediator(); // This comes from the custom Application class
... some type of listener to queue
}
private void getQueueData() {
byte[] tv = md.queueConsumer.poll();
// can't update textview get exception CalledFromWrongThreadException
((TextView) DisplayGraph.this.findViewById(R.id.tv)).setText("TV " + tv[0]);
}
}
推荐答案
处理这种情况的最佳方法是执行以下操作:上一个答案有错误.
Best way to handle this situation is to do this: Previous answer has errors in it.
public class MediatorData extends Observable {
/** Queue for the Activity */
LinkedBlockingQueue <byte[]> queueConsumer = new LinkedBlockingQueue <byte[]>();
/**
* Add data to a queue(s) for consumption
*/
public void put(byte[] data) throws InterruptedException {
queueConsumer.add(data);
notifyObservers();
}
/**
* Return data from the queue for the Feature calculations
*/
public byte[] getFeatureData() throws InterruptedException {
return queueConsumer.poll(100, TimeUnit.MILLISECONDS);
}
}
显示活动需要在UI线程上运行更新,因此请使用runOnUiThread方法.
Display activity needs to run the update on the UI Thread so use the runOnUiThread method.
public class DisplayGraph extends Activity implements Observer {
// populated from Application Class where its created
private MediatorData md;
byte[] tv;
public void onCreate(Bundle savedInstanceState) {
md = getMediator(); // This comes from the custom Application class
md.addObserver(this);
}
private void getQueueData() {
tv = md.queueConsumer.poll();
runOnUiThread(setRunnable);
}
public void update(Observable arg0, Object arg1) {
getQueueData();
}
// Need to do this to update the data to the UI.
final Runnable setImageRunnable = new Runnable() {
public void run() {
((TextView) DisplayGraph.this.findViewById(R.id.tv)).setText("TV " + tv[0]);
}
};
}
这篇关于从队列更新活动的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!