android 在Oreo中禁用状态栏拉入

zaqlnxep  于 2023-01-15  发布在  Android
关注(0)|答案(1)|浏览(110)

通过禁用状态栏的拖拽和点击来对应用程序进行kiosking的方法在android8上不起作用。
1.您可以在状态栏上放置一个窗口来禁用任何触摸或下拉操作。
正如这个答案所描述的,这种方法确实适用于android7及以下版本,但这种方法不适用于android8(oreo)。
我已经在Android 7和更少的系统上测试过了,它很好用,但是在Android 8上拉的时候状态栏仍然会拉下来。
如果您对此有解决方案,请提供帮助。
谢谢大家。

hc8w905p

hc8w905p1#

对于8和8以上的应用程序,你不能完全覆盖其他应用程序的视图,所以你必须做的是,当你拉下抽屉时,快速地将抽屉放回原处,这样用户就无法点击任何东西。这是实现这一点的代码。确保你在Activity中这样做。

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

然后,执行隐藏状态栏工作的helper类如下所示。

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

}

相关问题