繁体   English   中英

禁用Oreo中的状态栏拉

[英]Disable status bar pull in Oreo

通过禁用拉动和单击状态栏来自助应用程序的方法在Android 8上不起作用。 如何禁用状态栏在Android中单击并下拉?

  1. 您可以在状态栏上方放置一个窗口以禁用任何触摸或下拉。

正如这个答案所描述的,这种方法可以在android 7及以下版本中运行,但是这种方法在android 8(oreo)上不起作用。

我已经在android 7上测试了它并且它可以工作,但是在Android 8上拉动时状态栏仍然会拉下来。

如果您有解决方案请协助。

谢谢你们。

对于和8及以上你不能完全覆盖其他应用程序的视图,所以你需要做的是,当你拉下抽屉时,将抽屉快速返回到用户无法点击它上面的任何东西。 这是执行此操作的代码。 确保您在活动中执行此操作。

    @Override
public void onWindowFocusChanged(boolean hasFocus) {

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        if (!hasFocus) {

            Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
            sendBroadcast(closeDialog);


            // Method that handles loss of window focus
            new BlockStatusBar(this,false).collapseNow();
        }
    }
}

然后执行隐藏状态栏的辅助类如下所示。

public class BlockStatusBar {
Context context;

// To keep track of activity's window focus
boolean currentFocus;
// To keep track of activity's foreground/background status
boolean isPaused;

public static Handler collapseNotificationHandler;
Method collapseStatusBar = null;


public BlockStatusBar(Context context,boolean isPaused) {
    this.context=context;
    this.isPaused=isPaused;
    collapseNow();
}

public void collapseNow() {

    // 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 300 ms
        collapseNotificationHandler.postDelayed(new Runnable() {

            @Override
            public void run() {

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

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

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



                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();
                }

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

                if (!currentFocus && isPaused) {
                    collapseNotificationHandler.removeCallbacksAndMessages(null);
                }

            }
        }, 1L);
    }
}

}

暂无
暂无

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

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