title: How can I reliably simulate touch events on Android without root (like Automate and Tasker)?
body: <p>How can I reliably simulate touch events on Android (without rooting) from Java outside my app which runs as a background service?</p>
  
  <p>While this question has been asked before, <strong>most answers utilise ADB</strong>.
  (such as <a href="https://stackoverflow.com/questions/31911110/how-to-simulate-touch-events-on-android-device">How to simulate touch events on Android device?</a>)</p>
  
  <p><a href="https://github.com/chetbox/android-mouse-cursor" rel="noreferrer">https://github.com/chetbox/android-mouse-cursor</a> offers a good solution using Accessibility, but is not very reliable as not all views respond to it, and games do not respond at all most of the time.</p>
  
  <pre><code>private void click() {
    AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
    if (nodeInfo == null) return;
  
    AccessibilityNodeInfo nearestNodeToMouse = findSmallestNodeAtPoint(nodeInfo, cursorLayout.x, cursorLayout.y + 50);
  
    if (nearestNodeToMouse != null) {
      logNodeHierachy(nearestNodeToMouse, 0);
      nearestNodeToMouse.performAction(AccessibilityNodeInfo.ACTION_CLICK);
    }
  
    nodeInfo.recycle();
  }
  </code></pre>
  
  <blockquote>
    <p>This is the current code used by <a href="https://github.com/chetbox/android-mouse-cursor" rel="noreferrer">https://github.com/chetbox/android-mouse-cursor</a>.</p>
  </blockquote>
  
  <p>Android Version is 8.0, stock Android</p>
  
  <p>Is there a better, more reliable way to simulate these touch events from Java? Thanks in advance!</p>