简体   繁体   English

Oreo中的Android隐藏和禁用通知(状态)栏

[英]Android Hide & Disable Notification (Status) Bar in oreo

The method for kiosking an application by disabling pull and click of the status bar does not work on android 8. As anserwed on How to disable status bar click and pull down in Android? 通过禁用状态栏的单击和单击来为应用程序设置信息亭的方法在android 8上不起作用。如上如何在Android中禁用状态栏的单击和下拉菜单所述 .

I have tested it on android 7 and less and it works, but the status bar still pulls down when pulled on android 8. 我已经在android 7及更低版本上对其进行了测试,并且可以正常工作,但是在android 8上将其拉出时,状态栏仍然会拉低。

I didnt find any solution for the same. 我没有找到相同的任何解决方案。 Please let me know if there is any solution that works for oreo also. 请让我知道是否有适用于oreo的解决方案。

Thanks! 谢谢!

I am using below code in MainActivity . 我在MainActivity使用以下代码。

//Global Declaration
Handler collapseNotificationHandler;

@Override
    public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    Log.d(tag, "window focus changed");

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        collapseNow();
    }
}

Collapse Method 收合方法

public void collapseNow() {

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

        // Post a Runnable with some delay - currently set to 300 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(true);

                try {
                    collapseStatusBar.invoke(statusBarService);
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
                // Currently, the delay is 10 ms. You can change this
                // value to suit your needs.
                collapseNotificationHandler.postDelayed(this, 10L);
            }
        }, 10L);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM