android - 防止状态栏扩展

标签 android statusbar lockscreen

有没有办法阻止用户滑动状态栏(展开)或向后折叠?

我正在尝试更换锁屏,这似乎是一项必备功能。有没有什么方法可以在不需要root权限的情况下做到这一点?

最佳答案

您实际上可以防止状态栏扩展,而无需 root 手机。见 this link .这将绘制一个状态栏高度的窗口并消耗所有触摸事件。

在 onCreate() 之后调用以下代码。

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 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;
    //https://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);
}

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", "**********Intercepted");
        return true;
    }
}

关于android - 防止状态栏扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7457730/

相关文章:

android - 即使锁定屏幕也可以连续播放声音

python - 登录ubuntu之前如何在python守护进程中使用DBUS

android - 使用 Vuetify PWA 模板时如何更改 Android 状态栏颜色

c# - 在锁定屏幕上显示 UWP 控件

android - 如何分割AndroidManifest Android

android - Kotlin-添加更多变体-soundPool

ios - 禁用个人热点通知

crash - 如何使用 [[UIApplication sharedApplication] valueForKey : @"statusBar"] on ios13? 获取 wifi 信号强度

android:通过套接字传输文件

Android ImageReader 获取 NV21 格式?