沉浸式模式失败:是代码还是虚拟设备?

时间:2014-09-27 19:32:15

标签: android

我试图实现我的应用程序,只要你的手指在屏幕上,就会激活沉浸式模式。在我的Galaxy S5上,它就像我描述的那样工作。然而,在BlueStacks和Nexus 10的AVD上,它并不起作用。相反,只有ACTION_DOWN收到所有其他onTouchEvent(MotionEvent event)

当某些设备/虚拟设备工作而其他设备无法工作时,我发现很难分辨出错误。我的代码是否出现了触发沉浸式模式的问题?我低音地从here复制了它。

我发现如果删除行newUiOptions ^= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;,则会始终在Nexus 10上触发onTouchEvent,但导航栏当然不会被隐藏。

的onTouchEvent:

    public boolean onTouchEvent(MotionEvent event)
    {
        points.add(new FPoint(event.getX(), event.getY()));

        if (event.getY() > closeY - 35 && event.getY() < closeY && event.getX() < 50)
            tActivity.finish();

        if (event.getAction() == MotionEvent.ACTION_DOWN && isImmersiveModeEnabled)
            toggleHideyBar(tActivity);
        else if (event.getAction() == MotionEvent.ACTION_UP && !isImmersiveModeEnabled)
            toggleHideyBar(tActivity);

        return true;
    }

toggleHideyBar:

public void toggleHideyBar(ScreenTouchTest sttActivity)
{
    if (Build.VERSION.SDK_INT < 11)
        return;

    // The UI options currently enabled are represented by a bitfield.
    // getSystemUiVisibility() gives us that bitfield.
    int uiOptions = sttActivity.getWindow().getDecorView().getSystemUiVisibility();
    int newUiOptions = uiOptions;
    isImmersiveModeEnabled = ((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions);

    // Navigation bar hiding: Backwards compatible to ICS.
    if (Build.VERSION.SDK_INT >= 14)
    {
        newUiOptions ^= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
    }

    // Status bar hiding: Backwards compatible to Jellybean
    if (Build.VERSION.SDK_INT >= 16)
    {
        newUiOptions ^= View.SYSTEM_UI_FLAG_FULLSCREEN;
    }

    // Immersive mode: Backward compatible to KitKat.
    // Note that this flag doesn't do anything by itself, it only augments
    // the behavior of HIDE_NAVIGATION and FLAG_FULLSCREEN.
    // For the purposes of this
    // sample all three flags are being toggled together.
    // Note that there are two immersive mode UI flags, one of which is
    // referred to as "sticky".
    // Sticky immersive mode differs in that it makes the navigation and
    // status bars semi-transparent, and the UI flag does not get cleared when the user
    // interacts with the screen.
    if (Build.VERSION.SDK_INT >= 18)
    {
        newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
    }

    sttActivity.getWindow().getDecorView().setSystemUiVisibility(newUiOptions);
}

0 个答案:

没有答案