从队列更新活动的最佳方式

Best way to update Activity from a Queue(从队列更新活动的最佳方式)

本文介绍了从队列更新活动的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Producer-Mediator-Consumer"模型的 Mediator 中有一个 LinkedBlockingQueue.Producer 首先更新 Mediator 添加到 activityQueue.接下来,Consumer/Activity 等待/侦听队列并抓取下一项.

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.

我想要一个 Activity 来查看队列大小已更改并获取下一个项目.Mediator 无法看到 Activity,只有 Activity 可以看到 Mediator.那么如何创建我想要的这种监听器机制呢?

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]);
        }
    };
}

这篇关于从队列更新活动的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:从队列更新活动的最佳方式

基础教程推荐