阻止来自服务的触摸

时间:2016-06-11 22:48:06

标签: android android-activity service touch android-service

有没有办法阻止来自服务的任何应用程序的触摸?我一直在考虑getWindow(),但这在服务中是不可能的。另外,我一直在考虑获取当前活动,然后使用getWindow()。我希望你明白我想要实现的目标。

1 个答案:

答案 0 :(得分:0)

感谢David Medenjak,我设法解决了这个问题。此解决方案适用于23以下的Android API,它将使屏幕变黑并阻止触摸,直到您在屏幕上长按。

        WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        ImageView black = new ImageView(this);
        black.setImageResource(R.drawable.black);

        WindowManager.LayoutParams params = new     WindowManager.LayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                        WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                PixelFormat.TRANSLUCENT);

        params.gravity = Gravity.TOP | Gravity.LEFT;
        params.x = 0;
        params.y = 0;

        windowManager.addView(black, params);

        black.getLayoutParams().height = windowManager.getDefaultDisplay().getHeight();
        black.getLayoutParams().width = windowManager.getDefaultDisplay().getWidth();

        black.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                return false;
            }
        });