防止出现android的状态栏(修改)

Abh*_*aan 26 android kiosk statusbar

我正在实现一个信息亭模式应用程序,我已经成功地使应用程序全屏没有状态栏外观4.3但无法隐藏4.3和4.4中的状态栏,因为当我们向下滑动屏幕顶部时状态栏出现.

我试图让它全屏显示

  • 在清单中指定全屏主题
  • 设置窗口标志即setFlags
  • setSystemUiVisibility

可能重复但未找到具体解决方案

永久隐藏Android状态栏

最后我想要的是,如何在活动中永久隐藏状态栏?在android 4.3,4.4,5,6versions中

Abh*_*aan 86

我们无法阻止在kitkat设备中以全屏模式出现状态,因此制作了仍然符合要求的黑客攻击,即阻止状态栏扩展.

为了工作,应用程序没有全屏显示.我们在状态栏上放置了一个叠加层并消耗了所有输入事件.它阻止了地位的扩大.

注意:

  • customViewGroup是自定义类,可扩展任何布局(框架,相对布局等)并消耗触摸事件.
  • 使用touch事件覆盖视图组的onInterceptTouchEvent方法并返回true

更新

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> 
Run Code Online (Sandbox Code Playgroud)

customViewGroup实现

代码:

WindowManager manager = ((WindowManager) getApplicationContext()
            .getSystemService(Context.WINDOW_SERVICE));

WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams();
localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
localLayoutParams.gravity = Gravity.TOP;
localLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|

            // this is to enable the notification to recieve touch events
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |

            // Draws over status bar
            WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;

    localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
    localLayoutParams.height = (int) (50 * getResources()
            .getDisplayMetrics().scaledDensity);
    localLayoutParams.format = PixelFormat.TRANSPARENT;

    customViewGroup view = new customViewGroup(this);

    manager.addView(view, localLayoutParams);
Run Code Online (Sandbox Code Playgroud)

  • @Abhimaan在oreo +设备上,任何用户添加的窗口或视图都不能显示在状态栏上方,状态栏总是可以拉下来. (3认同)
  • @ user1416682这是一个非常基本的customViewGroup,可以帮助您入门.[链接](https://gist.github.com/sarme/7e4dc90e2478ade310e6) (2认同)
  • @AbhimaanMadhav,我没想到:)调用manager.removeView(view)对我来说已经足够了.顺便说一下,你可以用它来禁用软按钮吗?目前它对我不起作用. (2认同)
  • 不再适用于Android 8.0或Oreo设备. (2认同)

Ale*_*rov 17

在Android M中,您必须获得制作叠加层的额外权限.android.permission.SYSTEM_ALERT_WINDOW是不足够的!所以我使用了Abhimaan的答案中的代码,disableStatusBar()并且不得不打算打开正确的设置对话框.我还添加了删除视图,onDestroy()以便在应用退出时启用状态栏.我也将叠加高度减少到40,因为它似乎足够了.代码在此处使用5.1和6.0.

public static final int OVERLAY_PERMISSION_REQ_CODE = 4545;
protected CustomViewGroup blockingView = null;

@Override
protected void onDestroy() {

    super.onDestroy();

    if (blockingView!=null) {
        WindowManager manager = ((WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE));
        manager.removeView(blockingView);
    }
}


@Override
protected void onCreate(Bundle savedInstanceState) {

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

            if (!Settings.canDrawOverlays(this)) {
                Toast.makeText(this, "Please give my app this permission!", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,Uri.parse("package:" + getPackageName()));
                startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE);
            } else {
                disableStatusBar();
            }
        }
        else {
            disableStatusBar();
        }
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == OVERLAY_PERMISSION_REQ_CODE) {
        if (!Settings.canDrawOverlays(this)) {
            Toast.makeText(this, "User can access system settings without this permission!", Toast.LENGTH_SHORT).show();
        }
        else
        { disableStatusBar();
        }
    }
}

protected void disableStatusBar() {

    WindowManager manager = ((WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE));

    WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams();
    localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
    localLayoutParams.gravity = Gravity.TOP;
    localLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |

            // this is to enable the notification to receive touch events
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |

            // Draws over status bar
            WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;

    localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
    localLayoutParams.height = (int) (40 * getResources().getDisplayMetrics().scaledDensity);
    localLayoutParams.format = PixelFormat.TRANSPARENT;

    blockingView = new CustomViewGroup(this);
    manager.addView(blockingView, localLayoutParams);
}
Run Code Online (Sandbox Code Playgroud)

  • 它在oreo中不起作用也请为oreo支持 (3认同)