这是一个基于问题Edit 3的解决方案示例。
C#Windows Froms应用程序“ formMain.cs ”:
using System.Net.Sockets;private List<Point> lstPoints;private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { lstPoints = new List<Point>(); lstPoints.Add(new Point(e.X, e.Y)); }}private void PictureBox1_MouseMove(object sender, MouseEventArgs e){ if (e.Button == MouseButtons.Left) { lstPoints.Add(new Point(e.X, e.Y)); }}private void PictureBox1_MouseUp(object sender, MouseEventArgs e){ lstPoints.Add(new Point(e.X, e.Y)); StringBuilder sb = new StringBuilder(); foreach (Point obj in lstPoints) { sb.Append(Convert.ToString(obj) + ":"); } serverSocket.Send("MDRAWEVENT" + sb.ToString() + Environment.newline);}android service ” SocketBackground.java “:
import java.net.Socket;String xline;while (clientSocket.isConnected()) { BufferedReader xreader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(), StandardCharsets.UTF_8)); if (xreader.ready()) { while ((xline = xreader.readLine()) != null) { xline = xline.trim(); if (xline != null && !xline.trim().isEmpty()) { if (xline.contains("MDRAWEVENT")) { String coordinates = xline.replace("MDRAWEVENT", ""); String[] tokens = coordinates.split(Pattern.quote(":")); Point[] moviments = new Point[tokens.length]; for (int i = 0; i < tokens.length; i++) { String[] coordinates = tokens[i].replace("{", "").replace("}", "").split(","); int x = Integer.parseInt(coordinates[0].split("=")[1]); int y = Integer.parseInt(coordinates[1].split("=")[1]); moviments[i] = new Point(x, y); } MyAccessibilityService.instance.mouseDraw(moviments, 2000); } } } }}android
AccessibilityService
” MyAccessibilityService.java “:
public void mouseDraw(Point[] segments, int time) { if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { Path path = new Path(); path.moveTo(segments[0].x, segments[0].y); for (int i = 1; i < segments.length; i++) { path.lineTo(segments[i].x, segments[i].y); GestureDescription.StrokeDescription sd = new GestureDescription.StrokeDescription(path, 0, time); dispatchGesture(new GestureDescription.Builder().addStroke(sd).build(), new AccessibilityService.GestureResultCallback() { @Override public void onCompleted(GestureDescription gestureDescription) { super.onCompleted(gestureDescription); } @Override public void onCancelled(GestureDescription gestureDescription) { super.onCancelled(gestureDescription); } }, null); } }}


