安卓10源码环境 如何调用系统级 java api 来监听指定区域的触摸 app_process来执行 请给出代码
要调用系统级 Java API 来监听指定区域的触摸,需要使用 Android 的 InputManager 类。以下是一个示例代码:
- 创建一个名为 TouchListener.java 的类:
import android.hardware.input.InputManager;
import android.os.SystemClock;
import android.view.InputDevice;
import android.view.KeyEvent;
import android.view.MotionEvent;
public class TouchListener implements InputManager.InputDeviceListener {
private InputManager mInputManager;
private int mPointerId;
public TouchListener() {
mInputManager = (InputManager) getSystemService(INPUT_SERVICE);
}
public void start() {
mInputManager.registerInputDeviceListener(this, null);
}
public void stop() {
mInputManager.unregisterInputDeviceListener(this);
}
@Override
public void onInputDeviceAdded(int deviceId) {
InputDevice device = InputDevice.getDevice(deviceId);
if (device != null && device.getSources() == InputDevice.SOURCE_TOUCHSCREEN) {
mPointerId = device.getMotionRange(MotionEvent.AXIS_X).getMax() + 1;
}
}
@Override
public void onInputDeviceRemoved(int deviceId) {
// Do nothing
}
@Override
public void onInputDeviceChanged(int deviceId) {
// Do nothing
}
public void touch(int x, int y) {
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis();
MotionEvent event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, x, y, 0);
event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
event.setPointerId(mPointerId);
mInputManager.injectInputEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT);
eventTime = SystemClock.uptimeMillis();
event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, x, y, 0);
event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
event.setPointerId(mPointerId);
mInputManager.injectInputEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT);
}
public void swipe(int startX, int startY, int endX, int endY) {
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis();
MotionEvent event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, startX, startY, 0);
event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
event.setPointerId(mPointerId);
mInputManager.injectInputEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT);
eventTime = SystemClock.uptimeMillis();
event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, startX, startY, 0);
event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
event.setPointerId(mPointerId);
mInputManager.injectInputEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT);
eventTime = SystemClock.uptimeMillis();
event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, endX, endY, 0);
event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
event.setPointerId(mPointerId);
mInputManager.injectInputEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT);
eventTime = SystemClock.uptimeMillis();
event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, endX, endY, 0);
event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
event.setPointerId(mPointerId);
mInputManager.injectInputEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT);
}
public void pressHome() {
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis();
KeyEvent event = new KeyEvent(downTime, eventTime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HOME, 0);
mInputManager.injectInputEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT);
eventTime = SystemClock.uptimeMillis();
event = new KeyEvent(downTime, eventTime, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HOME, 0);
mInputManager.injectInputEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT);
}
public void pressBack() {
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis();
KeyEvent event = new KeyEvent(downTime, eventTime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK, 0);
mInputManager.injectInputEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT);
eventTime = SystemClock.uptimeMillis();
event = new KeyEvent(downTime, eventTime, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK, 0);
mInputManager.injectInputEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT);
}
}
- 在你的 app_process 中使用 TouchListener:
TouchListener touchListener = new TouchListener();
touchListener.start();
// 监听指定区域的触摸
touchListener.touch(x, y);
// 模拟滑动
touchListener.swipe(startX, startY, endX, endY);
// 模拟按下 Home 键
touchListener.pressHome();
// 模拟按下返回键
touchListener.pressBack();
touchListener.stop();
注意:这段代码需要在系统级别运行,因此需要 root 权限
原文地址: http://www.cveoy.top/t/topic/ckLP 著作权归作者所有。请勿转载和采集!