我创建的服务与手机的developper选项中的选项完全相同:
“指针位置:屏幕叠加显示当前触摸数据”
我有我的服务,一切都很好,唯一导致问题的是叠加。当我使用标志TYPE_SYSTEM_ALERT
时,覆盖会阻止用户的输入,但至少会检测到它们。相反,如果我使用标记TYPE_SYSTEM_OVERLAY
,则输入不会被叠加层考虑,而是直接通过。
我尝试了很多解决方案但是我被卡住了。我只想让用户的ACTION_DOWN
和ACTION_UP
通过我的服务中的叠加来计算dX和dY。
这是代码,位于服务类
中private WindowManager windowManager;
private View topLeftView;
public void onCreate() {
super.onCreate();
topLeftView = new View(this);
topLeftView.setOnTouchListener(this);
topLeftView.setBackgroundColor(0x33ff0000);
topLeftView.setClickable(false);
topLeftView.setFocusable(false);
topLeftView.setLongClickable(false);
topLeftView.setFocusableInTouchMode(false);
WindowManager.LayoutParams topLeftParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);
topLeftParams.gravity = Gravity.START | Gravity.TOP;
windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
windowManager.addView(topLeftView, topLeftParams);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d(TAG, "onTouch: down");
break;
case MotionEvent.ACTION_UP:
Log.d(TAG, "onTouch: up");
break;
}
return false;
}