Android Hide&禁用通知(状态)栏

时间:2016-01-03 21:02:14

标签: android

我已经能够通过使用下面的代码全屏隐藏通知栏

android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen"

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

但我要做的是完全禁用状态栏。我处于" kiosk模式"我想确保用户不能从顶部挡板上滑下手指。上述两种解决方案都可以隐藏通知栏,但它不适用于在应用程序中完全禁用它。

这可能吗?

2 个答案:

答案 0 :(得分:10)

而不是跟随其他答案的链接,这就是我所做的。

如果下拉(即使在全屏幕应用程序中),此解决方案也不允许用户“查看”处于“预览”状态的状态栏,但它不允许用户将状态栏拉到其完整状态状态以查看设置,通知等。

您必须先在let task = NSTask() task.launchPath = "/usr/bin/open" task.arguments = ["Safari"] task.launch()

中添加权限
AndroidManifest.xml

然后添加另一个名为<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> 的类(Java文件)并将此代码放入其中:

customViewGroup.java

完成这两项设置后,您可以将其添加到主import android.content.Context; import android.util.Log; import android.view.MotionEvent; import android.view.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) { Log.v("customViewGroup", "**********Intercepted"); return true; } }

onCreate()

此解决方案禁用始终关闭状态栏的功能,直到您的应用关闭。如果您不想每次都关闭应用,则必须在暂停时删除此操作。

积分从@Abhimaan Madhav

转到This Answer

答案 1 :(得分:1)

我认为永久禁用状态栏很困难。我也在研究相同的概念,并做了很多研发工作,发现下面的代码可能有用。如果用户尝试扩展状态栏,则将在几秒钟内将其撤回,并且也将在oreo上运行。我曾尝试过使用其他操作系统。

公共类BlockStatusBar {         上下文上下文;

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

    public 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) {

            Runnable myRunnable = new Runnable() {
                public void run() {
                    // do something
                    try {

                        // 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) {
                            Log.e(LOG_TAG, "" + e.getMessage());
                        }

                        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) {
                            Log.e(LOG_TAG, "" + e.getMessage());
                        }

                        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'kioskthread 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);
                        }
                    } catch (Exception e) {
                        Log.e("MSG", "" + e.getMessage());
                    }
                }
            };
            // Post a Runnable with some delay - currently set to 300 ms
            collapseNotificationHandler.postDelayed(myRunnable, 1L);

        }
    }
}