0

I'm just wondering to know if a can with a background service have co-ordinates of a touch screen event in all activities.
Such like that


*final TextView textView = (TextView)findViewById(R.id.textView);
final View touchView = findViewById(R.id.touchView);
touchView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
              textView.setText("Touch coordinates : " +String.valueOf(event.getX()) + "x" + String.valueOf(event.getY()));
     return true;}
but without any view by default.
Thanks
apaderno
  • 28,547
  • 16
  • 75
  • 90
IOjohn
  • 1
  • 1

2 Answers2

1

possible similar ideas to this problem:

  1. have all activities have the same logic that will send the touch events . you can have a base activity that will have this logic , which all of your activities extend.

  2. have an on top view which will collect the touch events , using a system alert permission . don't forget to close the activity that holds it when needed , though , since it will keep catching touch events even after the app is hidden.

here's a sample code to set a view on top:

final WindowManager.LayoutParams param=new WindowManager.LayoutParams();
param.flags=WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
final View view=findViewById(R.id.view1);
final ViewGroup parent=(ViewGroup)view.getParent();
parent.removeView(view);
param.format=PixelFormat.RGBA_8888;
param.type=WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
param.gravity=Gravity.TOP|Gravity.LEFT;
param.width=view.getLayoutParams().width;
param.height=view.getLayoutParams().height;
final WindowManager wmgr=(WindowManager)getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
wmgr.addView(view,param);
// TODO handle overlapping title bar and/or action bar
// TODO you must add logic to remove the view
// TODO you must use a special permission to use this method :android.permission.SYSTEM_ALERT_WINDOW
android developer
  • 114,585
  • 152
  • 739
  • 1,270
1

I'm just wondering to know if a can with a background service have co-ordinates of a touch screen event in all activities.

No, sorry. Your activities can pass information about touch events to a service, but a service cannot directly receive touch events.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491