在Oreo中禁用状态栏拉

时间:2018-07-31 06:50:55

标签: android android-8.0-oreo android-statusbar android-8.1-oreo

在Android 8上,无法通过禁用状态栏的拉动和单击来为应用程序添加信息的方法在android 8上不起作用。

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

如该答案所述,此方法确实适用于android 7及以下版本,但是此方法不适用于android 8(oreo)。

我已经在android 7及更低版本上对其进行了测试,并且可以正常工作,但是当在android 8上运行时,状态栏仍然会拉低。

如果您对此有解决方案,请提供帮助。

谢谢大家。

1 个答案:

答案 0 :(得分:5)

对于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);
    }
}

}

相关问题