Skip to content

Instantly share code, notes, and snippets.

@libern
Forked from jiangecho/SplashActivity.java
Created July 3, 2019 03:59
Show Gist options
  • Save libern/a5ea055fd72b8b8bb68278551f1a4523 to your computer and use it in GitHub Desktop.
Save libern/a5ea055fd72b8b8bb68278551f1a4523 to your computer and use it in GitHub Desktop.
simulate user clicks on the content of webview
int X_MAX = 412;
int Y_MAX = 50;
int x, y;
x = random.nextInt(X_MAX);
y = random.nextInt(Y_MAX);
// String htmlEventJs = "var ev = document.createEvent(\"HTMLEvents\"); " +
// "var el = document.elementFromPoint(%d,%d); " +
// "ev.initEvent('click',true,true);" +
// " el.dispatchEvent(ev);";
// htmlEventJs = String.format(htmlEventJs, x, y);
// webView.loadUrl("javascript:" + htmlEventJs);
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis() + random.nextInt(100);
// List of meta states found here: developer.android.com/reference/android/view/KeyEvent.html#getMetaState()
int metaState = 0;
MotionEvent me = MotionEvent.obtain(
downTime,
eventTime,
MotionEvent.ACTION_DOWN,
x,
y,
metaState
);
webView.dispatchTouchEvent(me);
me = MotionEvent.obtain(
downTime,
eventTime,
MotionEvent.ACTION_UP,
x,
y,
metaState
);
webView.dispatchTouchEvent(me);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment