1

I've been researching for the past few days about how to create an auto clicker for Android, but I haven't found any solution. I would like to do it rootless, as many Play Store related apps do.

This code allows me to programmatically command a click, but it doesn't work when I exit the app:

final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            Process p;
            StringBuffer output = new StringBuffer();
            try {
                p = Runtime.getRuntime().exec("input tap 950 581");
                BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            }
            catch (IOException  e) {
                e.printStackTrace();
            }

        }
    }, 5000);

Sorry for english.

2 Answers2

4

After a few tries, I finally got it. Thanks to @Vaibhav who advised me for a great link in the comments. Also, I sent an email to the company Macrorify KoK-CODE and they helped me understand how an auto clicker works. Thanks! =)

I'll leave the initial solution I found here (just single click) . I'm a beginner, so maybe I did something unnecessary.

The solution is to use dispatchGesture method.

First you create a directory (I called it xml) and a file like config_accessibility_service.xml and add:

<accessibility-service
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:accessibilityEventTypes="typeAllMask"
    android:accessibilityFeedbackType="feedbackGeneric"
    android:accessibilityFlags="flagDefault"
    android:canPerformGestures="true"
    android:description="@string/accessibility_service_description"/>

And modify AndroidManifest.xml with BIND_ACCESSIBILITY_SERVICE permission:

    <service
        android:name=".AutoClickService"
        android:enabled="true"
        android:label="@string/accessibility_service_name"
        android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
        android:exported="@string/accessibility_service_name">
        <intent-filter>
            <action android:name="android.accessibilityservice.AccessibilityService"/>
        </intent-filter>

        <meta-data
            android:name="android.accessibilityservice"
            android:resource="@xml/config_accessibility_service"/>
    </service>

My AutoClickService class:

public class AutoClickService extends AccessibilityService {

@Override
public void onCreate() {
    super.onCreate();
}


//apparently this method is called every time a event occurs
@Override
public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
    System.out.println("access event");
    //autoClick(2000, 100, 950, 581);
}


@Override
public void onServiceConnected() {
    super.onServiceConnected();
    autoClick(2000, 100, 950, 581);
}

@Override
public void onInterrupt() {

}

public void autoClick(int startTimeMs, int durationMs, int x, int y) {
    boolean isCalled = dispatchGesture(gestureDescription(startTimeMs, durationMs, x, y), null, null);
    System.out.println(isCalled);
}

public GestureDescription gestureDescription(int startTimeMs, int durationMs, int x, int y) {
    Path path = new Path();
    path.moveTo(x, y);
    return createGestureDescription(new GestureDescription.StrokeDescription(path, startTimeMs, durationMs));
}

public GestureDescription createGestureDescription(GestureDescription.StrokeDescription... strokes) {
    GestureDescription.Builder builder = new GestureDescription.Builder();
    for (GestureDescription.StrokeDescription stroke : strokes) {
        builder.addStroke(stroke);
    }
    return builder.build();
}

}

Finally you can verify user permission and run the service in MainActivity with:

public void checkAccessibilityServicePermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        int access = 0;
        try{
            access = Settings.Secure.getInt(this.getContentResolver(), android.provider.Settings.Secure.ACCESSIBILITY_ENABLED);
        } catch (Settings.SettingNotFoundException e){
            e.printStackTrace();
            //put a Toast
        }
        if (access == 0) {
            Intent myIntent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
            myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(myIntent);
        }
    }
}
-1

If you are trying to click a button programmatically, try using

private Button button;
button = (Button) findViewById(R.id.button2);
button.performClick();

To make it work when you exit the app, create a service.

public class BackgroundService extends Service {
  
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
  
        // code to be executed at the start of the service
        return START_STICKY;
    }
  
    @Override
  
    public void onDestroy() {
        super.onDestroy();
  
        // stop the process at the stop of the service
    }
  
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

Then, inside the Activity.java

startService(new Intent(this, BackgroundService.class)); // to start the service

stopService(new Intent(this, BackgroundService.class)); // to stop the service

Hope this helps!

Vaibhav
  • 117
  • 7