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 to programmatically trigger the touch event in android?
// Obtain MotionEvent object
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis() + 100;
float x = 0.0f;
float y = 0.0f;
// List of meta states found here: developer.android.com/reference/android/view/KeyEvent.html#getMetaState()
int metaState = 0;
MotionEvent motionEvent = MotionEvent.obtain(
downTime,
eventTime,
MotionEvent.ACTION_UP,
x,
y,
metaState
);
// Dispatch touch event to view
view.dispatchTouchEvent(motionEvent);
How to simulate touch from background service with sendevent or other way?
I can't execute the "sendevent" command, but found another way for myself, hope it will be helpfull for somebody.
For simulate touch I used sendPointerSync() from android.app.Instrumentation, that work only with "android.permission.INJECT_EVENTS" permission. For use it you should compile your app as a system app.
To do it you should follow next steps:
Getting files from android source:
root-of-android-source-tree/out/host//framework/signapk.jar
root-of-android-source-tree/build/target/product/security/platform.x509.pem
root-of-android-source-tree/build/target/product/security/platform.pk8
sign your app using getting files:
Command "java -jar signapk.jar platform.x509.pem platform.pk8 YourApp-unsigned.apk" YourApp-signed.apk.
- adb install YourApp-signed.apk
- Run your app
- Use "adb shell ps" to confirm that your app is running as system.
Code with touch simulating(new thread is necessary for simulation):
Thread thread = new Thread(){
@Override
public void run(){
Instrumentation m_Instrumentation = new Instrumentation();
m_Instrumentation.sendPointerSync(MotionEvent.obtain(
SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(),
MotionEvent.ACTION_DOWN,posx, posy, 0));
m_Instrumentation.sendPointerSync(MotionEvent.obtain(
SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(),
MotionEvent.ACTION_UP,width*4/5,height, 0));
}
};
Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yourapp"
**android:sharedUserId="android.uid.system"**
android:versionCode="1"
android:versionName="1.0" >
Using resources:
- Programmatically Injecting Events on Android – Part 1
- How to compile Android Application with system permissions
- Instruction for compile here
Related Topics
How to Check If Location Services Are Enabled
Include .So Library in APK in Android Studio
Android Material and Appcompat Manifest Merger Failed
Android - How to Disable the Click of Home Button
Deleting a Gallery Image After Camera Intent Photo Taken
Using Intent in an Android Application to Show Another Activity
How Set Maximum Date in Datepicker Dialog in Android
How to Connect to More Than One Firebase Database from an Android App
...Have You Declared This Activity in Your Androidmanifest.Xml
Nested Recycler View Height Doesn't Wrap Its Content
Android- Going Back to Previous Activity with Different Intent Value
How to Get an APK File from an Android Device
Resume Application and Stack from Notification
Why Are Nested Weights Bad For Performance? Alternatives
How to Change Font Face of Webview in Android
Android Studio Marks R in Red with Error Message "Cannot Resolve Symbol R", But Build Succeeds