我创建了一个TYPE_SYSTEM_OVERLAY
并且我希望使用service
.
覆盖工作正常,但问题是当我尝试将其合并到其中时它无法捕获触摸事件GestureDetector
。当我点击屏幕时,吐司消息“onDown”没有显示..
如果有人告诉我哪里出错了,我将不胜感激:(我已经尝试GestureDetector
在课堂之外实施,OverlayView
但也没有结果。
public class NUSLogService extends IntentService {
OverlayView mView;
@Override
public void onCreate() {
super.onCreate();
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.RIGHT | Gravity.BOTTOM;
params.setTitle("TouchLayer");
mView = new OverlayView(getApplicationContext());
final WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(mView, params);
}
@Override
public void onDestroy() {
super.onDestroy();
if(mView != null)
{
((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(mView );
mView = null;
}
}
//self-created overlay class
class OverlayView extends ViewGroup implements OnGestureListener{
private GestureDetector gestureScanner = new GestureDetector(this);
public OverlayView(Context context) {
super(context);
Toast.makeText(getContext(),"OverlayView", Toast.LENGTH_SHORT).show();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// return super.onTouchEvent(event);
return gestureScanner.onTouchEvent(event);
}
@Override
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
// TODO Auto-generated method stub
}
@Override
public boolean onDown(MotionEvent e) {
Toast.makeText(getContext(),"onDown", Toast.LENGTH_SHORT).show();
return false;
}
}