0

I am trying to listen on touch event from a service with the following code

  public void onCreate() {
        super.onCreate();
    //    Toast.makeText(getBaseContext(),"onCreate", Toast.LENGTH_LONG).show();
        mView = new HUDView(this);
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                PixelFormat.TRANSLUCENT);
        params.gravity = Gravity.RIGHT | Gravity.TOP;
        params.setTitle("Load Average");
        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        wm.addView(mView, params);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        //return super.onTouchEvent(event);

         final int action = ev.getAction();
         //   switch (action & MotionEvent.ACTION_MASK) {
         //   case MotionEvent.ACTION_DOWN: {
                final float x = ev.getX();
                final float y = ev.getY();
                Points p = new Points();
                p.setPoints(x,y);
                list.add(p);


        return true;
    }

This code is working perfectly on Android ver 2.3 but onTouchEvent is not getting invoked in ver 4.0 and 4.1. What could be the problem

I am trying to listen taps from a service, service is attached with a Activity. The activity is used as usual but service keep recording taps. when using TYPE_SYSTEM_OVERLAY its works very fine on Android 2.3 but on 4.1 the onTouchEvent is not invoked. When using TYPE_SYSTEM_ALERT onTouchEvent is invoked but the the touch event is not passed to the activity, and it becomes unresponsive.

Sanjay Singh
  • 349
  • 5
  • 18

1 Answers1

0

Try this:

findViewById(R.id.someView).setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent ev) {
        final int action = ev.getAction();
        final float x = ev.getX();
        final float y = ev.getY();
        Points p = new Points();
        p.setPoints(x,y);
        list.add(p);

        return true;        
    }
});
Micer
  • 8,731
  • 3
  • 79
  • 73