Android overlay to grab ALL touch, and pass them on?

别来无恙 提交于 2019-11-26 22:16:05
While-E

Found this documentation that pretty much states that it's not possible to do both: Android : Multi touch and TYPE_SYSTEM_OVERLAY

They discuss workarounds but I don't think any of them will actually work for exactly what I'm trying to do. Both given the events to the underlying app, and being able to snoop them to act upon them for myself.

To create an overlay view, when setting up the LayoutParams you need to set the type to TYPE_SYSTEM_OVERLAY and use the flag FLAG_WATCH_OUTSIDE_TOUCH. This presents a problem because as the Android documentation states: "you will not receive the full down/move/up gesture, only the location of the first down as an ACTION_OUTSIDE." In order to receive the full array of touch events you need to use the TYPE_SYSTEM_ALERT type, but this causes the overlay to take over the screen and stop interaction with other elements.

Anyone wants to disagree I'd love to hear good news :-D

You can use the GestureOverlayView, if you want to hide the lines it draws you can set the color to Transparent #00000000 so it doesn't show up, and then you can capture all touches, and gestures.

http://developer.android.com/resources/articles/gestures.html

I was looking for the same thing.

This flag does the trick for me FLAG_NOT_TOUCH_MODAL

Works on Android 7 and 8 setup as below.

The only action I've implemented so far is a touch to close the overlay window.

I also used code from here Example System Overlay Code on Github which was needed to get the events.

By the way Google Maps does a really nice job with this on Android 8. You can drag their overlay window around, resize it or close it. And all other apps work fine while it's up.

    var type = 0

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
    {
        type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
    }
    else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
    {
        type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
    }

    var flags = FLAG_NOT_TOUCH_MODAL

    mOverlayLayoutParams = WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, type, flags, PixelFormat.TRANSLUCENT)
Rico

This is not the optimal solution but it works. Restart the service toggling the flag not touchable. put boolean extra to Intent which is used to startservice to determine previous state toggle value. A better implementation would be to fire the intent on touch when the window is listening and after a fixed period when not listening.

public class bleh extends Service {
    public void onCteqwer(int i) {

        Context context; Class <bleh> context1 = bleh.class;

        WindowManager.LayoutParams params = null;
        WindowManager mang = (WindowManager) getSystemService(WINDOW_SERVICE);

        //check previous state of service
        if(i==0)
            params = new WindowManager.LayoutParams(arg0,arg1,arg2,arg3,arg4,arg5);

        if(i==1)
            params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,arg0,arg1,arg2,arg3,arg4);

        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        View mViw = inflater.inflate(arg, null);

        mViw.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });

        mang.addView(mViw, params);

        Intent z = new Intent(context, context1);

        if(i==0)
            z.putExtra("name", 1);
        if(i==1)
            z.putExtra("name", 0);

        stopSelf();
        startService(z);
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Bundle i=intent.getExtras();
        int userName = 0;

        if (i != null)
        {
            userName = i.getInt("name");
            onCteqwer(userName);
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!