好的,我会尝试在此处提供比我的第一条评论更多的信息。
所以首先,系统不应该允许这种行为,正如该线程中所讨论的那样:https://groups.google.com/forum/#!topic/android-platform/d6Kt1DhCAtw
话虽如此,当我愿意对我的应用程序进行自动化 UI 测试时遇到了这个问题,尤其是对于图像处理,我想在屏幕上模拟手指。
这里有一个 sn-p,您可以将其应用于您的 Activity B:
第 1 步:将最后一个 MotionEvent 的 X 和 Y 坐标从 Activity A 传递到 Activity B
第 2 步:在Activity B 中检索这些值,然后应用:
private void continueScrolling(int posX, int posY) {
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis();
MotionEvent.PointerCoords[] pointerCoords = new MotionEvent.PointerCoords[1];
MotionEvent.PointerCoords pc1 = new MotionEvent.PointerCoords();
pc1.x = posX;
pc1.y = posY;
pc1.pressure = 1;
pc1.size = 1;
pointerCoords[0] = pc1;
MotionEvent.PointerProperties[] pointerProperties = new MotionEvent.PointerProperties[1];
MotionEvent.PointerProperties pp1 = new MotionEvent.PointerProperties();
pp1.id = 0;
pp1.toolType = MotionEvent.TOOL_TYPE_FINGER;
pointerProperties[0] = pp1;
MotionEvent event;
// send the initial touches (this seems to be to "wake up" the view, you might not need it in a non-testing context though)
event = MotionEvent.obtain(downTime, eventTime,
MotionEvent.ACTION_DOWN, 1, pointerProperties, pointerCoords,
0, 0, // metaState, buttonState
1, // x precision
1, // y precision
0, 0, // deviceId, edgeFlags
InputDevice.SOURCE_TOUCHSCREEN, 0); // source, flags
theViewYouWantTomove.dispatchGenericMotionEvent(event);
event = MotionEvent.obtain(downTime, eventTime,
MotionEvent.ACTION_DOWN,
1, pointerProperties, pointerCoords, 0, 0, 1, 1, 0, 0,
InputDevice.SOURCE_TOUCHSCREEN, 0);
theViewYouWantTomove.dispatchGenericMotionEvent(event);
}