Get co-ordinates touch screen in a background service

后端 未结 2 1676
萌比男神i
萌比男神i 2020-12-18 18:01

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 TextVie         


        
相关标签:
2条回答
  • 2020-12-18 18:12

    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.

    0 讨论(0)
  • 2020-12-18 18:30

    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
    
    0 讨论(0)
提交回复
热议问题