从套接字连接更新视图

发布于 2024-11-02 01:25:09 字数 160 浏览 1 评论 0原文

我正在编写一个应用程序,需要每 10 秒左右从套接字接收数据,然后在屏幕上绘制一个视图以绘制该数据。不幸的是,我对 Android 还很陌生,并且在理解如何使其工作方面遇到一些困难。我一直在阅读一些有关处理程序的文章,但我不太确定如何使用它们。您可以将它们与扩展视图的类一起使用,还是我根本不需要使用它们?

I'm writing an application that needs to receive data from a socket every 10 seconds or so, and then draw a view on the screen to graph that data. Unfortunately, I'm pretty new to Android and am having some trouble understanding how to make this work. I've been doing some reading on handlers, but I'm not quite sure how to make use of them. Can you use these with a class that extends a View, or do I not need to use these at all?

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

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

发布评论

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

评论(1

夏雨凉 2024-11-09 01:25:09

使用处理程序似乎是正确的方法,因为您正在从非 UI 线程更新 UI(至少您应该如此!)...您可以像这样使用处理程序,或者从 Activity 进行数据处理并调用来自 Activity 中处理程序的 CustomView.draw() 。

public class CustomView extends View {

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public CustomView(Context context) {
        super(context);
        ...
        startThread();
        ...
    }

    @Override
    public void onDraw(Canvas canvas) {
        ...
        //Do drawing...canvas.drawBitmap(bitmap) or w/e
        ...
    }

    private void startThread() {
        Thread thread = new Thread() {
            public void run() {
                try {
                    doSocketRequest();
                } catch (SocketException e) { // Or w/e exceptions are applicable
                    e.printStackTrace();
                } finally {
                    Message msg = new Message();
                    //msg.obj = ObjectContainingInformationTheHandlerMightNeed
                    mHandler.sendMessage(msg);//replace 0 w/ a message if need be

                }
            }
        };
    }

    private void doSocketRequest() {
        ...
        //Do your socket stuff here
        ...
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            ...
            //Do graph processing stuff
            ...
            invalidate(); //forces onDraw
        }
    };

}

Using a Handler seems to be the correct approach since you are updating your UI from a non UI thread (at least you should be!)...You can either use the Handler like this, or do the data processing from your Activity and call CustomView.draw() from a Handler in your Activity.

public class CustomView extends View {

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public CustomView(Context context) {
        super(context);
        ...
        startThread();
        ...
    }

    @Override
    public void onDraw(Canvas canvas) {
        ...
        //Do drawing...canvas.drawBitmap(bitmap) or w/e
        ...
    }

    private void startThread() {
        Thread thread = new Thread() {
            public void run() {
                try {
                    doSocketRequest();
                } catch (SocketException e) { // Or w/e exceptions are applicable
                    e.printStackTrace();
                } finally {
                    Message msg = new Message();
                    //msg.obj = ObjectContainingInformationTheHandlerMightNeed
                    mHandler.sendMessage(msg);//replace 0 w/ a message if need be

                }
            }
        };
    }

    private void doSocketRequest() {
        ...
        //Do your socket stuff here
        ...
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            ...
            //Do graph processing stuff
            ...
            invalidate(); //forces onDraw
        }
    };

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文