Denizen
Denizen

Reputation: 335

Receiving Touch Events from a Service

I am currently working on recieving touch events from a service.

One way I thought to do this was to use a TouchDelegate on a system view, extend its bounds to the screen, and then receive touch events from within the delegate.

However, when I use

((Activity)ctx).getWindow().getDecorView().setTouchDelegate(new TouchDel(bounds, view));

from within my service I get a nullpointerexception and a force close.

Upvotes: 1

Views: 1028

Answers (1)

devunwired
devunwired

Reputation: 63293

There really is no mechanism by which a Service could be directly registered to globally receive touch events from the system. The Activity UI in your application would need to hand the events it receives down to your Service. Without knowing more about the specific use you have for them, I can only suggest a generic approach. One such possible approach might be:

  • In your Activity classes, override dispatchTouchEvent() to be notified of each event as it is sent to your application. Be careful not to override the return behavior, which will disrupt the touch handling in your views.
  • MotionEvent is Parcelable, so you could package each event up in an Intent and send them as start commands to your Service.
  • Alternately, implement your Service as a bound service so each Activity in your application can bind to it and call methods directly, and have each MotionEvent passed that way.

Upvotes: 1

Related Questions