Best way to update Activity from a Queue

独自空忆成欢 提交于 2019-12-04 14:16:29

How about using Observer and Observable?

Could be something like this:

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);
        setChanged();
        notifyObservers();
    }

    /**
     * Return data from the queue for the Feature calculations
     */
    public byte[] getFeatureData() throws InterruptedException {
        return queueConsumer.poll(100, TimeUnit.MILLISECONDS);
    }
}

And this:

public class DisplayGraph extends Activity implements Observer {

    // populated from Application Class where its created
    private MediatorData md;

    public void onCreate(Bundle savedInstanceState) {
        md = getMediator();  // This comes from the custom Application class
        md.addObserver(this);
    }

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

    public void update(Observable arg0, Object arg1) {
        getQueueData();
    }
}

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

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]);
        }
    };
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!