在Android平板电脑/手机中阻止/禁用状态栏

时间:2015-11-06 18:47:39

标签: android kiosk-mode

我见过Kioware和SureLock应用程序。他们只是阻止平板电脑中的每个控件我知道覆盖后退按钮以及处理主页和最近的任务选项。 但我不确定他们是如何设法控制系统栏上的设置选项的。设置显示几分钟后消失。同样地,在向下滑动时出现的移动状态栏需要被阻止。

如果有人对此有所了解,请分享。任何指导/帮助都表示赞赏。

See the attached image selected portion which I need to block/disable

3 个答案:

答案 0 :(得分:3)

诀窍是添加覆盖在系统栏上的透明自定义视图,这样,自定义视图就会消耗所有用户交互,如触摸,拖动。

这是一个有效的代码:

windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);

statusBarOverlay = new CustomViewGroup(this);

final WindowManager.LayoutParams localLayoutParams =   
         new WindowManager.LayoutParams();

localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
localLayoutParams.gravity = Gravity.TOP;
localLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |

        // this is to enable the notification to receive touch events
        WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |

        // Draws over status bar
        WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;

localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
localLayoutParams.height =   
   (int) (50 * getResources().getDisplayMetrics().scaledDensity);
localLayoutParams.format = PixelFormat.TRANSPARENT;

windowManager.addView(statusBarOverlay, localLayoutParams);

自定义ViewGroup

public class CustomViewGroup extends ViewGroup {

        public CustomViewGroup(Context context) {
            super(context);
        }

        @Override
        protected void onLayout(boolean changed, int l, int t, int r, int b) {
        }

        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            return true;
        }
    }

我自己已经为我的应用程序完成了锁定功能,尽管我已将锁定功能分离到另一个应用程序中,原因有很多。您可以要求提供更多信息,如有必要,我会将它们放在这里。

答案 1 :(得分:2)

您可以尝试使用View(请按this示例)在所需位置添加透明WindowManager。通过这种方式,您将拦截来自用户的任何点击事件。

请务必设置正确的标记/类型,因为您需要覆盖系统栏:

    WindowManager.LayoutParams dismissParams = new WindowManager.LayoutParams(
                        viewWidth,
                        viewHeight,
                        WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, //you can also try with TYPE_SYSTEM_OVERLAY, TYPE_SYSTEM_ERROR
                        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                        PixelFormat.TRANSPARENT);

答案 2 :(得分:0)

最后我找到了上述问题的解决方案。 onWindowFocus更改我刚关闭系统生成的Dialogs,我的问题解决了。下面是示例代码。

@Override
     public void onWindowFocusChanged(boolean hasFocus) {
         super.onWindowFocusChanged(hasFocus);

      if(!hasFocus)
      {// Close every kind of system dialog 
          Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); 
          sendBroadcast(closeDialog); 

      }

     }

更新:

在移动设备中阻止屏幕上显示的状态栏使用以下方式。

private Handler collapseNotificationHandler;
            @Override
            public void onWindowFocusChanged(boolean hasFocus) {
                boolean currentFocus = hasFocus;
                if (!hasFocus) {
                    collapseNow(true,currentFocus);

                }

            }


    public void collapseNow(final boolean shouldCollapse, final boolean currentFocus ) {

                // Initialize 'collapseNotificationHandler'
                if (collapseNotificationHandler == null) {
                    collapseNotificationHandler = new Handler();
                }

                // If window focus has been lost && activity is not in a paused state
                // Its a valid check because showing of notification panel
                // steals the focus from current activity's window, but does not 
                // 'pause' the activity
                if (!currentFocus && !isPaused) {

                    // Post a Runnable with some delay - currently set to 50 ms
                    collapseNotificationHandler.postDelayed(new Runnable() {

                        @Override
                        public void run() {

                            // Use reflection to trigger a method from 'StatusBarManager'                

                            Object statusBarService = getSystemService("statusbar");
                            Class<?> statusBarManager = null;

                            try {
                                statusBarManager = Class.forName("android.app.StatusBarManager");
                            } catch (ClassNotFoundException e) {
                                e.printStackTrace();
                            }

                            Method collapseStatusBar = null;

                            try {

                                // Prior to API 17, the method to call is 'collapse()'
                                // API 17 onwards, the method to call is `collapsePanels()`

                                if (Build.VERSION.SDK_INT > 16) {
                                    collapseStatusBar = statusBarManager.getMethod("collapsePanels");
                                } else {
                                    collapseStatusBar = statusBarManager.getMethod("collapse");
                                }
                            } catch (NoSuchMethodException e) {
                                e.printStackTrace();
                            }

                            collapseStatusBar.setAccessible(shouldCollapse);

                            try {
                                collapseStatusBar.invoke(statusBarService);
                            } catch (IllegalArgumentException e) {
                                e.printStackTrace();
                            } catch (IllegalAccessException e) {
                                e.printStackTrace();
                            } catch (InvocationTargetException e) {
                                e.printStackTrace();
                            }

                            // Check if the window focus has been returned
                            // If it hasn't been returned, post this Runnable again
                            // Currently, the delay is 50 ms. You can change this
                            // value to suit your needs.
                            if (!currentFocus && !isPaused) {
                                collapseNotificationHandler.postDelayed(this, 50);
                            }

                        }
                    }, 0);
                }   
            }

&#34; isPaused得到&#34;当app处于Pause状态时设置为true的boolean。

相关问题