1

android上有没有办法保留状态栏,同时禁用你可以用它做的所有交互,比如把它拉下来?

我想保留此栏提供的信息,但我不希望用户与之交互。

4

1 回答 1

1

这是我喜欢使用的方法。您可以从方法中解开它并将其放置在基础 Activity 中。iirc,我也从 StackOverflow 得到了这个,但我没有记下它,所以我不确定原始帖子在哪里。

它的基本作用是在顶部栏上放置一个透明覆盖层,拦截所有触摸事件。到目前为止,它对我来说效果很好,看看它对你有用。

您可能需要将此行放在 AndroidManifest 中:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

我的项目中有它,但我不记得是因为这个还是其他原因。如果您收到权限错误,请将其添加进去。

WindowManager manager;
CustomViewGroup lockView;

public void lock(Activity activity) {

    //lock top notification bar
    manager = ((WindowManager) activity.getApplicationContext()
            .getSystemService(Context.WINDOW_SERVICE));

    WindowManager.LayoutParams topBlockParams = new WindowManager.LayoutParams();
    topBlockParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
    topBlockParams.gravity = Gravity.TOP;
    topBlockParams.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;
    topBlockParams.width = WindowManager.LayoutParams.MATCH_PARENT;
    topBlockParams.height = (int) (50 * activity.getResources()
            .getDisplayMetrics().scaledDensity);
    topBlockParams.format = PixelFormat.TRANSPARENT;

    lockView = new CustomViewGroup(activity);
    manager.addView(lockView, topBlockParams);
}

和 CustomViewGroup 是

private class CustomViewGroup extends ViewGroup {
    Context context;

    public CustomViewGroup(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
    }
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        Log.i("StatusBarBlocker", "intercepted by "+ this.toString());
        return true;
    }
}

还!当您的活动结束时,您还必须删除此视图,因为我认为即使您终止应用程序,它也会继续阻塞屏幕。总是,总是总是把它称为 onPause 和 onDestroy。

    if (lockView!=null) {
        if (lockView.isShown()) {
            //unlock top
            manager.removeView(lockView);
        }
    }
于 2015-05-15T09:54:41.077 回答