<small id='nYDGa'></small><noframes id='nYDGa'>

    • <bdo id='nYDGa'></bdo><ul id='nYDGa'></ul>

    1. <i id='nYDGa'><tr id='nYDGa'><dt id='nYDGa'><q id='nYDGa'><span id='nYDGa'><b id='nYDGa'><form id='nYDGa'><ins id='nYDGa'></ins><ul id='nYDGa'></ul><sub id='nYDGa'></sub></form><legend id='nYDGa'></legend><bdo id='nYDGa'><pre id='nYDGa'><center id='nYDGa'></center></pre></bdo></b><th id='nYDGa'></th></span></q></dt></tr></i><div id='nYDGa'><tfoot id='nYDGa'></tfoot><dl id='nYDGa'><fieldset id='nYDGa'></fieldset></dl></div>
      <legend id='nYDGa'><style id='nYDGa'><dir id='nYDGa'><q id='nYDGa'></q></dir></style></legend>
      <tfoot id='nYDGa'></tfoot>
    2. 从队列更新活动的最佳方式

      Best way to update Activity from a Queue(从队列更新活动的最佳方式)
        <bdo id='rD4hD'></bdo><ul id='rD4hD'></ul>

            <tbody id='rD4hD'></tbody>
        • <small id='rD4hD'></small><noframes id='rD4hD'>

          1. <legend id='rD4hD'><style id='rD4hD'><dir id='rD4hD'><q id='rD4hD'></q></dir></style></legend><tfoot id='rD4hD'></tfoot>
          2. <i id='rD4hD'><tr id='rD4hD'><dt id='rD4hD'><q id='rD4hD'><span id='rD4hD'><b id='rD4hD'><form id='rD4hD'><ins id='rD4hD'></ins><ul id='rD4hD'></ul><sub id='rD4hD'></sub></form><legend id='rD4hD'></legend><bdo id='rD4hD'><pre id='rD4hD'><center id='rD4hD'></center></pre></bdo></b><th id='rD4hD'></th></span></q></dt></tr></i><div id='rD4hD'><tfoot id='rD4hD'></tfoot><dl id='rD4hD'><fieldset id='rD4hD'></fieldset></dl></div>

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

                问题描述

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

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

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Sqlite database not copied from asset folder Android(Sqlite 数据库未从资产文件夹 Android 复制)
                SQLite Database Copy Appears Corrupted When Generated by Device and not Emulator(SQLite 数据库副本在由设备而不是模拟器生成时出现损坏)
                Android file copy(安卓文件拷贝)
                Android how to detect Copy event of Edittext in android(Android如何在android中检测Edittext的Copy事件)
                Android copy image from gallery folder onto SD Card alternative folder(Android将图像从图库文件夹复制到SD卡替代文件夹)
                Is there a tool to find unused resources in an Android project?(是否有工具可以在 Android 项目中查找未使用的资源?)
                    <tfoot id='hc8Xb'></tfoot>
                      <tbody id='hc8Xb'></tbody>

                    <small id='hc8Xb'></small><noframes id='hc8Xb'>

                      <bdo id='hc8Xb'></bdo><ul id='hc8Xb'></ul>
                    • <i id='hc8Xb'><tr id='hc8Xb'><dt id='hc8Xb'><q id='hc8Xb'><span id='hc8Xb'><b id='hc8Xb'><form id='hc8Xb'><ins id='hc8Xb'></ins><ul id='hc8Xb'></ul><sub id='hc8Xb'></sub></form><legend id='hc8Xb'></legend><bdo id='hc8Xb'><pre id='hc8Xb'><center id='hc8Xb'></center></pre></bdo></b><th id='hc8Xb'></th></span></q></dt></tr></i><div id='hc8Xb'><tfoot id='hc8Xb'></tfoot><dl id='hc8Xb'><fieldset id='hc8Xb'></fieldset></dl></div>
                    • <legend id='hc8Xb'><style id='hc8Xb'><dir id='hc8Xb'><q id='hc8Xb'></q></dir></style></legend>