Android:如何从UI线程执行网络(套接字)相关的操作?

时间:2018-09-01 13:54:25

标签: java android sockets android-asynctask ui-thread

我正在尝试监听触摸事件并根据触摸事件的类型流对象。但是,只要我触摸屏幕,应用程序就会崩溃。请给我替代方法。

@Override
        public boolean onTouchEvent(MotionEvent event) {
            final float x = event.getX();
            final float y = event.getY();

            try {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        touch_start(x, y);
                        stream.writeObject(new Action(x,y,Action.DOWN));
                        invalidate();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        touch_move(x, y);
                        stream.writeObject(new Action(x,y,Action.MOVE));
                        invalidate();
                        break;
                    case MotionEvent.ACTION_UP:
                        touch_up();
                        stream.writeObject(new Action(x,y,Action.UP));
                        invalidate();
                        break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return true;
        }

0 个答案:

没有答案