我不知道这是否真的是一个问题,但我创建了一个应用程序,通过 wifi 或便携式热点控制计算机上的鼠标光标。问题是:当我用 wifi 接入点连接我的电脑和我的安卓设备时,光标移动不如我将电脑连接到安卓设备的便携式热点时流畅。我尝试了两部不同的安卓手机,也遇到了同样的问题。以下是我在应用程序上使用的 TCP 代码:
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
xStart = event.getRawX();
yStart = event.getRawY();
if (mess.equals("click")) {
mess="leftdown";
}
break;
case MotionEvent.ACTION_MOVE:
xMove = event.getRawX();
yMove = event.getRawY();
distanceX = (xStart - xMove);
distanceY = (yStart - yMove);
sendCmd(distanceX, distanceY);
xStart = xMove;
yStart = yMove;
mess="move";
sendMess(mess);
break;
case MotionEvent.ACTION_UP:
break;
}
return detector.onTouchEvent(event);
}
void sendCmd(float distanceX, float distanceY) {
try {
out.writeFloat(distanceX);
out.writeFloat(distanceY);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
void sendMess(String mess) {
try {
out.writeUTF(mess);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
这是我在服务器上使用的 TCP 代码
@Override
public void run() {
// System.out.println("Connected");
// pixel, velocity
float x = 0, y = 0;
try {
Robot mouse = new Robot();
PointerInfo a;
Point b;
String mess;
int xx, yy;
DataInputStream in = new DataInputStream(server.getInputStream());
x = in.readFloat();
y = in.readFloat();
mess = in.readUTF();
System.out.println(x + "-" + y);
if (mess.equals("click")) {
mouse.mousePress(InputEvent.BUTTON1_MASK);
mouse.mouseRelease(InputEvent.BUTTON1_MASK);
} else if (mess.equals("scroll")) {
} else if (mess.equals("leftdown")) {
mouse.mousePress(InputEvent.BUTTON1_MASK);
} else if (mess.equals("leftup")) {
mouse.mouseRelease(InputEvent.BUTTON1_MASK);
} else if (mess.equals("right")) {
mouse.mousePress(InputEvent.BUTTON3_MASK);
mouse.mouseRelease(InputEvent.BUTTON3_MASK);
} else if (mess.equals("move")) {
a = MouseInfo.getPointerInfo();
b = a.getLocation();
xx = (int) b.getX();
yy = (int) b.getY();
mouse.mouseMove(xx - (int) x, yy - (int) y);
mouse.delay(7);
}
} catch (Exception e) {
// TODO: handle exception
}
}
由于某些特殊原因,请原谅我无法在我的 java 文件中发布整个代码。