Reputation: 21
I am trying to simulate a click function in an android application utilizing the Accessability Service. I have set up the service in the manifest, and enabled it in my phones settings. I have a class MouseAccessabilityService which contains a the following click function:
// Clikc function that clicks at a specific x and y coordinate
@RequiresApi(api = Build.VERSION_CODES.N)
public static GestureDescription click(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();
}
I am calling the click function in the MainActivity via a button press that triggers the click (for testing purposes):
Buttonclick = (Button)findViewById(R.id.clickbutton);
Buttonclick.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onClick(View view) {
//changeButtonText.setText("Second Text");
MouseAccessabilityService.click(centerX, centerY);
}
});
The X and Y is being passed as an integer variable from some other functionality. I can not seem to get the click() function to work from within my app and press on a button.
Am I on the right page? Can this functionality even work? I am at a standstill with this and would appreciate any help.
Thanks.
Upvotes: 2
Views: 4907
Reputation: 36
You can use the dispatchGesture function.
Try replacing your click function's return with :
return dispatchGesture(clickBuilder.build(), null, null);
https://codelabs.developers.google.com/codelabs/developing-android-a11y-service/#7
Upvotes: 1