我创建了一个TYPE_SYSTEM_OVERLAY
,我希望使用service
捕获此叠加层上的手指/触摸事件。
叠加层工作正常,但问题是当我尝试将GestureDetector
合并到其中时无法捕获触摸事件。当我点击屏幕时,toast msg“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;
}
}
答案 0 :(得分:2)
首先,这不应该是IntentService
。在您当前的实现中,onDestroy()
将在onCreate()
之后被称为毫秒或两秒。
其次,出于安全原因,TYPE_SYSTEM_OVERLAY
无法接收Android 4.0中的任何触摸事件。