I did auto clicker using dispatchGesture and everything is working but when I touch the screen using my finger it stop and call onCancelled method
The Service
public class MyService extends AccessibilityService {
private Path path;
private GestureDescription.Builder builder;
private GestureDescription.StrokeDescription strokeDescription;
private GestureResultCallback gestureResultCallback;
private GestureDescription gestureDescription;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.getBooleanExtra("isClicked", false)) {
clickOnPosition();
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
}
@Override
public void onInterrupt() {
}
private void clickOnPosition() {
if (path == null) {
path = new Path();
path.moveTo(540, 960);
path.lineTo(540, 960);
}
if (builder == null)
builder = new GestureDescription.Builder();
if (strokeDescription == null) {
strokeDescription = new GestureDescription.StrokeDescription(path, 0, 500);
builder.addStroke(strokeDescription);
}
if (gestureResultCallback == null)
gestureResultCallback = new GestureResultCallback() {
@Override
public void onCompleted(GestureDescription gestureDescription) {
clickOnPosition();
}
@Override
public void onCancelled(GestureDescription gestureDescription) {
super.onCancelled(gestureDescription);
}
};
if (gestureDescription == null)
gestureDescription = builder.build();
dispatchGesture(gestureDescription, gestureResultCallback, null);
}
}
Button In MainActivity
public void click(View view) {
if (intent == null) {
intent = new Intent(this, MyService.class);
intent.putExtra("isClicked", true);
startService(intent);
}
}
And about code is it good code or there is a better way to do this one???
I'm very surprised that you are starting AccessibilityService
by own (in click
method)... this kind of Service
must be enabled, allowed and started by system automatically, check out how to declare this in HERE. without that process (system auto-start, not own call) AccessibilityService
will probably behave like usual Service
, causing no permission for executing dispatchGesture
(and all other sensitive methods call), also some custom AccessibilityService
methods won't be ever called, like overriden, but empty onAccessibilityEvent(...)