Detecting when system buttons are visible while using 'immersive mode'

后端 未结 4 600
谎友^
谎友^ 2020-12-28 08:39

I\'m currently using immersive mode (API 19) for one of my Activities as follows:

getWindow().getDecorView()
            .setSystemUiVisibility(
                     


        
4条回答
  •  隐瞒了意图╮
    2020-12-28 09:11

    I have found a solution that suits me even though it's not perfect. I set the UI visiblity to View.SYSTEM_UI_FLAG_IMMERSIVE instead of View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY and when I receive the onSystemUiVisibilityChange callback, I delay a message to an Handler to reset the UI Visibility. Here is the code :

    private static final int FULL_SREEN_MSG = 10;
    private static final long TIME_BEFORE_HIDE_UI = 2 * DateUtils.SECOND_IN_MILLIS;
    
    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == FULL_SREEN_MSG) {
                setFullscreen();
            }
        }
    };
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setFullscreen();
        getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(this);
    }
    
    private void setFullscreen() {
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
                        | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
                        | View.SYSTEM_UI_FLAG_IMMERSIVE);
    }
    
    @Override
    public void onSystemUiVisibilityChange(int visibility) {
        if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
            mHandler.sendEmptyMessageDelayed(FULL_SREEN_MSG, TIME_BEFORE_HIDE_UI);
        }
    }
    

提交回复
热议问题