Android 中的观察者模式

发布于 2024-09-14 03:49:51 字数 330 浏览 3 评论 0原文

我有一个问题。
1.我有两个线程:“worker”和“UI”线程。
2. Worker持续等待服务器端的数据,获取到后通知UI线程。
3. 更新 UI 时,屏幕上会显示 Toast 消息。

第 3 步有问题,如下所示:

android.view.ViewRoot$CalledFromWrongThreadException:仅 创建视图层次结构的原始线程可以触摸其视图。

使用 mHandler,runOnUIThread 会减慢 UI 线程(UI 显示 webview),因为我必须不断检查来自服务器的数据。

I have an issue.
1. I have two threads: 'worker' and 'UI' thread.
2. Worker keeps on waiting for data from server, when gets it notifies to UI thread.
3. On update UI shows Toast message on screen.

Step 3 is problem as it says:

android.view.ViewRoot$CalledFromWrongThreadException: Only the
original thread that created a view hierarchy can touch its views.

Using mHandler, runOnUIThread slows down the UI thread (UI displays webview), as I have to continuously check for data from server.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初见终念 2024-09-21 03:49:51

使用 AsyncTask 来实现这一点。重写 doInBackground 来获取数据(它在单独的线程上执行),然后重写 onPostExecute() 以显示 toast(它在 UI 线程上执行)。

这是一个很好的例子 http://www.screaming-penguin.com/node/7746

这里是官方文档

UPD:有关如何处理部分进度的示例。

    class ExampleTask extends AsyncTask<String, String, String>{

    @Override
    protected String doInBackground(String... params) {
        while(true){
            //Some logic on data recieve..
            this.publishProgress("Some progress");
            //seee if need to stop the thread.
            boolean stop = true;
            if(stop){
                break;
            }
        }
        return "Result";
    }

    @Override
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);
        //UI tasks on particular progress...
    }
}

Use AsyncTask to implement this. Override doInBackground to get the data (it is executed on the separate thread), then override onPostExecute() to show the toast (it is executed on the UI thread).

Here is good example http://www.screaming-penguin.com/node/7746

And here is official docs.

UPD: Example on how to handle partial progress.

    class ExampleTask extends AsyncTask<String, String, String>{

    @Override
    protected String doInBackground(String... params) {
        while(true){
            //Some logic on data recieve..
            this.publishProgress("Some progress");
            //seee if need to stop the thread.
            boolean stop = true;
            if(stop){
                break;
            }
        }
        return "Result";
    }

    @Override
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);
        //UI tasks on particular progress...
    }
}
靖瑶 2024-09-21 03:49:51

我会使用一项服务,并将您的活动绑定到该服务。然后服务可以在有新数据时发送广播

I would use a service, and bind your activity to the service. Then the service can send a broadcast when it has new data

⊕婉儿 2024-09-21 03:49:51

Android 中的对象观察者模式?

定义: 观察者模式定义了对象之间的一对多依赖关系,以便当一个对象更改状态时,它的所有依赖项都会收到通知,并且自动更新。

       The objects which are watching the state changes are called observer. Alternatively observer are also called listener. The object which is being watched is called subject.

Example: View A is the subject. View A displays the temperature of a     container.  View B display a green light is the temperature is above 20 degree Celsius. Therefore View B registers itself as a Listener to View A.  If the temperature of View A is changed an event is triggered. That is event is send to all registered listeners in this example View B. View B receives the changed data and can adjust his display.

 Evaluation:  The subject can registered an unlimited number of observers. If a new listener should register at the subject no code change in the subject is necessary.

Object Observer pattern in Android ?

Definition: The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically.

       The objects which are watching the state changes are called observer. Alternatively observer are also called listener. The object which is being watched is called subject.

Example: View A is the subject. View A displays the temperature of a     container.  View B display a green light is the temperature is above 20 degree Celsius. Therefore View B registers itself as a Listener to View A.  If the temperature of View A is changed an event is triggered. That is event is send to all registered listeners in this example View B. View B receives the changed data and can adjust his display.

 Evaluation:  The subject can registered an unlimited number of observers. If a new listener should register at the subject no code change in the subject is necessary.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文