How can I reliably simulate touch events on Android without root (like Automate and Tasker)?
As suggested, the best way to simulate touch events since Nougat (API 24) is by using an accessibility service and the AccessibilityService#dispatchGesture method.
Here is how I did to simulate a single tap event.
// (x, y) in screen coordinates
private static GestureDescription createClick(float x, float y) {
// for a single tap a duration of 1 ms is enough
final int DURATION = 1;
Path clickPath = new Path();
clickPath.moveTo(x, y);
GestureDescription.StrokeDescription clickStroke =
new GestureDescription.StrokeDescription(clickPath, 0, DURATION);
GestureDescription.Builder clickBuilder = new GestureDescription.Builder();
clickBuilder.addStroke(clickStroke);
return clickBuilder.build();
}
// callback invoked either when the gesture has been completed or cancelled
callback = new AccessibilityService.GestureResultCallback() {
@Override
public void onCompleted(GestureDescription gestureDescription) {
super.onCompleted(gestureDescription);
Log.d(TAG, "gesture completed");
}
@Override
public void onCancelled(GestureDescription gestureDescription) {
super.onCancelled(gestureDescription);
Log.d(TAG, "gesture cancelled");
}
};
// accessibilityService: contains a reference to an accessibility service
// callback: can be null if you don't care about gesture termination
boolean result = accessibilityService.dispatchGesture(createClick(x, y), callback, null);
Log.d(TAG, "Gesture dispatched? " + result);
To perform other gestures, you might find useful the code used for testing the AccessibilityService#dispatchGesture implementation.
EDIT: I link a post in my blog with an introduction to Android accessibility services.
How calc X, Y coordinates to simulate touch on screen?
The solution was found and the reference was this answer:
procedure TForm2.Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
Index, XTouch, YTouch, RXCoord, RYCoord: Integer;
List: TStrings;
RScreen: String;
begin
Index := Form1.ListView1.ItemIndex;
if Index = -1 then
Exit;
List := TStringList.Create;
RScreen := Form1.ListView1.Selected.SubItems[6];
try
ExtractStrings(['x'], [], PChar(RScreen), List); // Ex: my smartphone is 1920x1080
RYCoord := StrToInt(List[0]); // 1920 (height)
RXCoord := StrToInt(List[1]); // 1080 (width)
finally
List.Free;
end;
XTouch := Round((X / Image1.Width) * RXCoord);
YTouch := Round((Y / Image1.Height) * RYCoord);
Form1.SS1.Socket.Connections[Index].SendText('touch' + IntToStr(XTouch)
+ '<|>' + IntToStr(YTouch) + #13#10);
end;
Related Topics
Android Onconfigurationchanged Not Being Called
Android Unable to Instantiate Activity: Didn't Find Class on Path
Difference Between Actionbarsherlock and Actionbar Compatibility
How to Give an Imageview Click Effect Like a Button on Android
Android: How to Handle Button Click
Is There Any Library or Algorithm for Persian (Shamsi or Jalali) Calendar in Android
How to Create Your Own Library for Android Development to Be Used in Every Program You Write
How to Get Count of Number Methods Used in a Jar File
Unsupported Major.Minor Version 52.0 in My App
How to Pass Values Between a Dialog and an Activity
Requestlegacyexternalstorage Is Not Working in Android 11 - API 30
Android Overlay a View Ontop of Everything
Wait Until Firebase Retrieves Data
Variable Length (Dynamic) Arrays in Java
Capture and Log the Response Body