我正在实现一个kiosk模式应用程序,我已经成功地使应用程序全屏没有状态栏外观4.3,但无法隐藏状态栏4.3和4.4当我们在屏幕顶部向下滑动时状态栏出现
我已经尝试通过
使其全屏- 在清单 中指定全屏主题
- 设置窗口标志,如setFlags
- setSystemUiVisibility
可能重复,但未找到具体解决方案
永久隐藏Android状态栏
最后我想要的是,如何隐藏状态栏永久在一个活动??android 4.3、4.4、5、6版本
我们无法阻止kitkat设备在全屏模式下显示状态,所以我们做了一个仍然符合要求的hack,即阻止状态栏展开
要做到这一点,应用程序没有全屏显示。我们在状态栏上放置了一个覆盖层,并消耗了所有输入事件。它阻止状态扩展。
注意:
- customViewGroup是扩展any的自定义类布局(框架,相对布局等)并消耗触摸事件。 的onInterceptTouchEvent方法返回true
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
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);
在Android M中,你必须获得额外的权限才能进行覆盖。android.permission.SYSTEM_ALERT_WINDOW
是不够的!所以我使用了disableStatusBar()
中Abhimaan的答案中的代码,并且必须做出打开正确设置对话框的意图。我还在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);
}
对于我所从事的一个项目,我已经找到了一个解决方案,但它花了很长时间。在Stackoverflow和其他地方的各种线程帮助我想出了它。这是Android M上的一个解决方案,但它运行得很好。由于有人要求,所以我想我应该把它贴在这里,如果它能对任何人有益。
现在已经有一段时间了,我不记得所有的细节,但是CustomViewGroup是覆盖主ViewGroup的类,并检测用户从顶部滑动到显示状态栏。但是我们不想显示它,所以用户的拦截被检测到,任何进一步的动作都被忽略,即Android操作系统不会得到打开隐藏状态栏的信号。
然后还包括显示和隐藏状态栏的方法,您可以将其复制/粘贴到您想要显示/隐藏状态栏的代码中。
/**
* This class creates the overlay on the status bar which stops it from expanding.
*/
public static 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", "********** Status bar swipe intercepted");
return true;
}
}
public static void allowStatusBarExpansion(Context context) {
CustomViewGroup view = new CustomViewGroup(context);
WindowManager manager = ((WindowManager) context.getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE));
manager.removeView(view);
}
// Stop expansion of the status bar on swipe down.
public static void preventStatusBarExpansion(Context context) {
WindowManager manager = ((WindowManager) context.getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE));
Activity activity = (Activity) context;
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;
//http://stackoverflow.com/questions/1016896/get-screen-dimensions-in-pixels
int resId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
int result = 0;
if (resId > 0) {
result = activity.getResources().getDimensionPixelSize(resId);
}
localLayoutParams.height = result;
localLayoutParams.format = PixelFormat.TRANSPARENT;
CustomViewGroup view = new CustomViewGroup(context);
manager.addView(view, localLayoutParams);
}