赞
踩
禁用下拉拉出消息通知栏,禁用上拉拉出虚拟按键栏
此例用kotlin来写
方法网上都有,有几个错误点要注意
要引入系统的framework.jar或者在系统源码中编译,不然会编译出错
Unresolved reference: DISABLE_EXPAND
- private var statusBarManager: StatusBarManager = context.getSystemService("statusbar") as StatusBarManager
-
- // 禁用
- statusBarManager.disable(StatusBarManager.DISABLE_EXPAND)
- // 还原
- statusBarManager.disable(StatusBarManager.DISABLE_NONE)
要加上权限
- <!-- status bar -->
- <uses-permission android:name="android.permission.EXPAND_STATUS_BAR"/>
- <uses-permission android:name="android.permission.STATUS_BAR"
- tools:ignore="ProtectedPermissions" /> <!--状态栏修改-->
否则会出错
java.lang.SecurityException: StatusBarManagerService: Neither user 10167 nor current process has android.permission.STATUS_BAR.
此例用java来写
- /**
- * 用反射实现控制StatusBar状态,需要是system应用
- */
- public class StatusBarUtil {
- public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
- public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
- public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
- public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
- public static final int STATUS_BAR_DISABLE_NONE = 0x00000000;
-
- public static void setStatusBarState(Context context, int disableFlag) {
- @SuppressLint("WrongConstant") Object service = context.getSystemService("statusbar");
- try {
- Class<?> statusbarManager = Class.forName("android.app.StatusBarManager");
- Method disable = statusbarManager.getMethod("disable", int.class);
- disable.invoke(service, disableFlag);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- /**
- * 禁用顶部状态栏的下拉及底部虚拟的回退等按键
- */
- public static void disableStatusBar(Context context) {
- // Disable *all* possible navigation via the system bar.
- int state = STATUS_BAR_DISABLE_EXPAND | STATUS_BAR_DISABLE_HOME | STATUS_BAR_DISABLE_RECENT | STATUS_BAR_DISABLE_BACK;
- setStatusBarState(context, state);
- }
-
- public static void enableStatusBar(Context context) {
- setStatusBarState(context, STATUS_BAR_DISABLE_NONE);
- }
-
- }
单纯地禁用上下拉,不能满屏显示,虚拟键盘显示的地方会是全黑无法显示,还需要额外的操作
- //在Ui中调用此函数,确保满屏隐藏虚拟键盘
- public void makeWindowFullScreen() {
- this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
- WindowManager.LayoutParams.FLAG_FULLSCREEN);
- getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
- | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
- | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
- | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
- | View.SYSTEM_UI_FLAG_FULLSCREEN
- | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
- );
- }
转载于:https://blog.csdn.net/zmlovelx/article/details/125207511
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。