I am developing an auto-clicker application for Android. I have already managed to create floating buttons etc. Now I am trying to simulate a touch event.
My code of service looks as below:
class MyAccessibilityService : AccessibilityService() {
override fun onInterrupt() {}
override fun onAccessibilityEvent(event: AccessibilityEvent?) {}
override fun onServiceConnected() {
super.onServiceConnected()
}
// Method to simulate a touch event
fun simulateTouchEvent(x: Float, y: Float) {
val gesture = GestureDescription.Builder().addStroke(
GestureDescription.StrokeDescription(
Path().apply { moveTo(x, y) },
50,
50,
)
)
val result = dispatchGesture(gesture.build(), null, null)
if (!result) {
Log.d("TAG", "Failed to dispatch gesture.")
}
}
}
This is my AndroidManifest.xml
:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=";
xmlns:tools=";>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.TestApplication"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.TestApplication">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".OverTheTopService">
</service>
<service
android:name=".ClickAccessibilityService"
android:exported="false"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
<meta-data
android:name="android.accessibilityservice"
android:resource="@xml/accessibility_service_config" />
<meta-data
android:name="android.accessibilityservice.AccessibilityService_canPerformGestures"
android:value="true" />
</service>
</application>
</manifest>
And this is my accessibility_service_config.xml
:
<accessibility-service xmlns:android=";
android:canPerformGestures="true" />
The problem is that function dispatchGesture
always returns false. I have accessibility permisions etc. enabled on my mobile after installing the application. I also tried to add a callback to this function, but it was never invoked. The coordinates I pass are correct and fits in device dimensions.
What else can be wrong with this code? How to properly simulate touch event in the context of floating buttons where simulated events should be done under my application?