检测到statusBar已崩溃

时间:2014-12-09 15:05:23

标签: android statusbar

我在我的应用按钮中实现了扩展statusBar,尽管statusBar已隐藏。为了实现这一点,我在OnClick中执行此操作:

@Override
 public void onClick(View view) {
    Utils.showTitleBar(ctx, false);
    Utils.setNotification(ctx);
}

这项工作很好,但是,我想在statusBar崩溃时隐藏statusBar,并且实现这一点,我实现了这个:

@Override
    public void onWindowFocusChanged(boolean hasFocus) {
        Log.i("TAG", "  boolen:" + Boolean.toString(hasFocus));
        if (hasFocus && pref.getBoolean("belStatus", false))
            Utils.showTitleBar(ctx, true);
    }

这项工作也很好但只有一次。在下次我按下按钮并展开statusBar时,当我崩溃时,' onWindowDocousChanged'从来没有打电话,所以statusBar仍然存在。 那么,还有另一种检测statusBar崩溃的方法吗?或者有人可以帮我解释为什么“onWindowFocousChanged'没有第二次打电话?

如果它有用,我使用的方法是:

 public static void setNotification(Context ctx){
        Object sbservice = ctx.getSystemService( "statusbar" );
        Class<?> statusbarManager;
        try {
            statusbarManager = Class.forName("android.app.StatusBarManager");
            Method showsb;
            if (Build.VERSION.SDK_INT >= 17) {
                showsb = statusbarManager.getMethod("expandNotificationsPanel");
            }
            else {
                showsb = statusbarManager.getMethod("expand");
            }
            showsb.invoke( sbservice );
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

 public static void showBelTitleBar(MainActivity ctx, boolean show){
        View status = ctx.findViewById(R.id.status_bar);
        if (show) {
            if (Build.VERSION.SDK_INT < 16) {
                ctx.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                        WindowManager.LayoutParams.FLAG_FULLSCREEN);
            } else {
                ctx.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
            }

        } else {
            if (Build.VERSION.SDK_INT < 16) {
                ctx.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
            } else {
                ctx.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
            }
        }

    }

1 个答案:

答案 0 :(得分:1)

如果它对某人有帮助,我通过在通知栏的开头添加一些延迟来实现我的目标。这样,onWindowFocusChanged()方法总是根据需要调用。 所以现在我的代码以这种方式实现:

findViewById(R.id.status_bar).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent e) {
                Utils.showBelTitleBar(ctx, false, updateClkBtry, handler);
                ctx.handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Utils.setNotification(ctx);
                    }
                }, 100);
                return false;
            }
        });