Here's the answer to a similar question Get co-ordinates touch screen in a background service:
No, sorry. Your activities can pass information about touch events to
a service, but a service cannot directly receive touch events
But you can only receive touch events with an activity running.
Here's how you would do a touch listener in an activity:
ImageView playLayout = (ImageView)findViewById(R.id.playLayout);
playLayout.setKeepScreenOn(true);
playLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent me) {
float newY = me.getY();
float newX = me.getX();
int action = me.getAction();
if (action==0){
Log.v(tag, "New Touch");
Log.i(tag, String.valueOf("Action " + action +
" happened at X,Y: " + "("+newX+","+newY + ")"));
}
return true;
}
});
Of course you can add more to it. There, I'm just logging the x and y coordinates of where the touch happened.