在 Lollipop 上隐藏系统 UI

Hiding the System UI on Lollipop

我有一个 kiosk 模式应用程序,它隐藏了系统的所有痕迹 UI(通知栏和导航按钮)。在 Lollipop 之前的 Android 版本上,以下工作正常(作为 root):

service call activity 42 s16 com.android.systemui

然而,在 Lollipop 中,这会使屏幕完全变黑并隐藏系统 UI。因此无法使用。

有人知道解决这个问题的方法吗?

我已经尝试了用于屏幕固定的设备 Owner/Admin 解决方案,但不幸的是,这是不可接受的,因为它没有完全隐藏系统 UI,但在从屏幕底部。

如果设备已获得 root 权限,您可以禁用 systemui pm disable-user com.android.systemui,然后设备所有者方法可以正常工作。

如果设备运行其他应用程序,则不应使用此方法,因为如果您的应用程序崩溃,systemui 可能会被禁用并且用户无法与设备交互。

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
&device-owner package="com.mycompany" name="*mycompany" />

您可能想使用 'Immersive mode'。您会找到所有需要的信息 here

对于 Android !

的每个版本,您都有一个示例 here

我遇到了同样的问题。此代码适用于 4.4 和 Lollipop(已测试)。也应该适用于其他版本。

 /**
     * Uses Root access to enable and disable SystemUI.
     * @param enabled Decide whether to enable or disable.
     */
    public void setSystemUIEnabled(boolean enabled){
        try {
            Process p = Runtime.getRuntime().exec("su");
            DataOutputStream os = new DataOutputStream(p.getOutputStream());
            os.writeBytes("pm " + (enabled ? "enable" : "disable")
                    + " com.android.systemui\n");
            os.writeBytes("exit\n");
            os.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

当然需要root

我找到了另一个解决方案,它适用于 android 5.0

private void hideNavigationBar(){
        try {
            Process process = Runtime.getRuntime().exec("su");
            DataOutputStream os = new DataOutputStream(process.getOutputStream());
            os.writeBytes("pm disable com.android.systemui\n");
            os.flush();
            os.writeBytes("exit\n");
            os.flush();
            process.waitFor();
            //////////////////////////////////////
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

我使用以下设置来摆脱 Android Marshmallow

中的系统 UI
final int flags = 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
        | View.SYSTEM_UI_FLAG_FULLSCREEN
        | View.SYSTEM_UI_FLAG_IMMERSIVE;

我有 OnCreate

this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_instructions);
getWindow().getDecorView().setSystemUiVisibility(flags);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

然后我添加了代码以在用户滑动顶部或底部屏幕时隐藏后退键。

/* Hides Navigation bar again if user swipes it visible */
@Override
protected void onResume() {
        getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(
                new View.OnSystemUiVisibilityChangeListener() {
                    @Override
                    public void onSystemUiVisibilityChange(int visibility) {
                    getWindow().getDecorView().setSystemUiVisibility(flags);
                    }
                });
    super.onResume();
    Log.i(TAG,"Activity Life Cycle : onResume : MainActivity Resumed");
}

这将在屏幕上短暂显示返回键并将其隐藏。 唯一的问题是,如果您在 Pinned activity 之上打开新的 Activity。它没有任何 onResume 指示。