我整天都在处理这个问题。问题在于,当我尝试在乐器浓缩咖啡测试中拖动某些东西时,我会遇到以下错误。
Caused by: android.support.test.espresso.InjectEventSecurityException: java.lang.SecurityException: Injecting to another application requires INJECT_EVENTS permission
当我调用此方法
时发生这种情况 onView(withId(R.id.any_id)).perform(CustomViewActions.touchAndDrag(200, 200));
自定义方法
public static ViewAction touchAndDrag(final float x, final float y, final long delay) {
return new ViewAction() {
@Override
public void perform(UiController uiController, final View view) {
// Get view absolute position
sendLinearSwipe(uiController,coordinatesClickOn,coordinatesMoveTo,precision,2000);
};
}
和 swipeLinaer
是从浓缩咖啡来源
private static Swiper.Status sendLinearSwipe(UiController uiController, float[] startCoordinates,
float[] endCoordinates, float[] precision, int duration) {
checkNotNull(uiController);
checkNotNull(startCoordinates);
checkNotNull(endCoordinates);
checkNotNull(precision);
float[][] steps = interpolate(startCoordinates, endCoordinates, SWIPE_EVENT_COUNT);
final int delayBetweenMovements = duration / steps.length;
MotionEvent downEvent = MotionEvents.sendDown(uiController, startCoordinates, precision).down;
try {
for (int i = 0; i < steps.length; i++) {
if (!MotionEvents.sendMovement(uiController, downEvent, steps[i])) {
Log.e(TAG, "Injection of move event as part of the swipe failed. Sending cancel event.");
MotionEvents.sendCancel(uiController, downEvent);
return Swiper.Status.FAILURE;
}
long desiredTime = downEvent.getDownTime() + delayBetweenMovements * i;
long timeUntilDesired = desiredTime - SystemClock.uptimeMillis();
if (timeUntilDesired > 10) {
uiController.loopMainThreadForAtLeast(timeUntilDesired);
}
}
if (!MotionEvents.sendUp(uiController, downEvent, endCoordinates)) {
Log.e(TAG, "Injection of up event as part of the swipe failed. Sending cancel event.");
MotionEvents.sendCancel(uiController, downEvent);
return Swiper.Status.FAILURE;
}
} finally {
downEvent.recycle();
}
return Swiper.Status.SUCCESS;
}
问题是,仅当touch 侦听器设置
时,这是不起作用的喜欢
anyView.setOnTouchListener(new MyTouchListener());
所以设置了哪种侦听器,但设置的事实确实无关紧要。
private final class MyTouchListener implements View.OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(
view);
view.startDrag(data, shadowBuilder, view, 0);
view.setVisibility(View.INVISIBLE);
return true;
} else {
return false;
}
}
}
我不知道如何解决这个问题。
我将感谢任何帮助或建议。
android管理刷牙对象的方式不太清楚。当您尝试拖动某些东西时,Android会生成一张遵循手指的东西的图片,并为您提供拖动的想法。问题在于,产生该效果的系统的组件不是来自您的应用程序,而是来自另一个应用程序,因此Android相信,在拖动过程中,您正在触摸另一个"应用程序"。偶然触摸系统键盘时出现同样的问题。
该解决方案是使用系统证书签名APK,并在清单上添加Inpote_events的权限。