之前遇到的问题:向系统发送一个长按触摸事件,再发送其他触摸事件时之前的长按事件消失。不多说,直接上代码,用到了测试类Instrumentation,可在上层直接执行,但仅限于当前程序!
package com.zfibs.touch;
import android.app.Instrumentation;
import android.os.SystemClock;
import android.util.Log;
import android.view.MotionEvent;
import android.view.MotionEvent.PointerCoords;
import android.view.MotionEvent.PointerProperties;
public class Test {
//向系统注入两点触摸事件
private String TAG = "__Test__";
PointerProperties[] properties = new PointerProperties[2];
PointerProperties pp1 = new PointerProperties();
PointerProperties pp2 = new PointerProperties();
PointerCoords[] pointerCoords = new PointerCoords[2];
Test() {
}
public void handleTouch(String args[]) throws Exception {
String[] mArgs = args;
try {
String opt = mArgs[0];
if (opt.equals("onetouch")) {
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis();
float x = Float.valueOf(mArgs[1]);
float y = Float.valueOf(mArgs[2]);
int type = Integer.valueOf(mArgs[mArgs.length - 1]);
PointerProperties pp1 = new PointerProperties();
pp1.id = 0;
pp1.toolType = MotionEvent.TOOL_TYPE_FINGER;
properties[0] = pp1;
PointerCoords pc1 = new PointerCoords();
pc1.x = x;
pc1.y = y;
pc1.pressure = 1;
pc1.size = 1;
pointerCoords[0] = pc1;
Log.i(TAG, "__one PointTouch__" + x + "___" + y + "___");
MotionEvent event = null;
if(type == 0){
event = MotionEvent.obtain(downTime, eventTime,
MotionEvent.ACTION_DOWN, 1, properties, pointerCoords,
0, 0, 1, 1, 0, 0, 0, 0);
}else{
event = MotionEvent.obtain(downTime, eventTime,
MotionEvent.ACTION_UP, 1, properties, pointerCoords,
0, 0, 1, 1, 0, 0, 0, 0);
}
sendPointerTest(event);
} else if (opt.equals("twotouch")) {
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis();
float x = Float.valueOf(mArgs[1]);
float y = Float.valueOf(mArgs[2]);
int type = Integer.valueOf(mArgs[mArgs.length - 1]);
PointerProperties pp2 = new PointerProperties();
pp2.id = 1;
pp2.toolType = MotionEvent.TOOL_TYPE_FINGER;
properties[1] = pp2;
PointerCoords pc2 = new PointerCoords();
pc2.x = x;
pc2.y = y;
pc2.pressure = 1;
pc2.size = 1;
pointerCoords[1] = pc2;
Log.i(TAG, "__two PointTouch__" + x + "___" + y + "___");
MotionEvent event = null;
if (type == 0) {
event = MotionEvent.obtain(downTime, eventTime,
MotionEvent.ACTION_POINTER_2_DOWN, 2, properties,
pointerCoords, 0, 0, 1, 1, 0, 0, 0, 0);
} else {
event = MotionEvent.obtain(downTime, eventTime,
MotionEvent.ACTION_POINTER_2_UP, 2, properties,
pointerCoords, 0, 0, 1, 1, 0, 0, 0, 0);
}
sendPointerTest(event);
}
} catch (RuntimeException e) {
e.printStackTrace();
}
}
public static void sendPointerTest(MotionEvent event) {
try {
Instrumentation inst = new Instrumentation();
inst.sendPointerSync(event);
} catch (Exception e) {
e.printStackTrace();
}
}
}
如果要跨进程发送模拟事件,需调用:
private static void sendPointerSync(MotionEvent event) {
try {
(IWindowManager.Stub.asInterface(ServiceManager
.getService("window"))).injectPointerEvent(event, true);
} catch (RemoteException e) {
e.printStackTrace();
}
}
需在源码中编译生成jar包,root权限下降jar导入/system/framework/下,方可调用。