4

阅读https://developer.android.com/training/system-ui/immersive.html上的文档,我似乎找不到任何关于如何在 Native Activity NDK 应用程序中设置全屏沉浸模式的信息(不使用 JNI ) 因为似乎全屏沉浸模式只能从 Java 切换。

由于无法从清单中设置(在清单中设置 Android 沉浸式全屏模式),有没有办法通过 EGL 请求它?

似乎启用全屏沉浸式模式的唯一方法是在通过 EGL 请求表面之前通过 JNI 调用 setSystemUiVisibility?

4

1 回答 1

2

回答我自己的问题,您可以通过 C++/JNI 设置沉浸式模式,而无需将 java 添加到您的项目中。这是我的片段,大部分是从互联网上的某个地方复制的。

auto portis::android_util::SetImmersiveMode(JNIEnv* env, android_app* iandroid_app) -> bool {
    PORTIS_ASSERT(iandroid_app && env);

    jclass activityClass = env->FindClass("android/app/NativeActivity");
    jclass windowClass = env->FindClass("android/view/Window");
    jclass viewClass = env->FindClass("android/view/View");
    jmethodID getWindow = env->GetMethodID(activityClass, "getWindow", "()Landroid/view/Window;");
    jmethodID getDecorView = env->GetMethodID(windowClass, "getDecorView", "()Landroid/view/View;");
    jmethodID setSystemUiVisibility = env->GetMethodID(viewClass, "setSystemUiVisibility", "(I)V");
    jmethodID getSystemUiVisibility = env->GetMethodID(viewClass, "getSystemUiVisibility", "()I");

    jobject windowObj = env->CallObjectMethod(iandroid_app->activity->clazz, getWindow);
    jobject decorViewObj = env->CallObjectMethod(windowObj, getDecorView);

    // Get flag ids
    jfieldID id_SYSTEM_UI_FLAG_LAYOUT_STABLE = env->GetStaticFieldID(viewClass, "SYSTEM_UI_FLAG_LAYOUT_STABLE", "I");
    jfieldID id_SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = env->GetStaticFieldID(viewClass, "SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION", "I");
    jfieldID id_SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = env->GetStaticFieldID(viewClass, "SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN", "I");
    jfieldID id_SYSTEM_UI_FLAG_HIDE_NAVIGATION = env->GetStaticFieldID(viewClass, "SYSTEM_UI_FLAG_HIDE_NAVIGATION", "I");
    jfieldID id_SYSTEM_UI_FLAG_FULLSCREEN = env->GetStaticFieldID(viewClass, "SYSTEM_UI_FLAG_FULLSCREEN", "I");
    jfieldID id_SYSTEM_UI_FLAG_IMMERSIVE_STICKY = env->GetStaticFieldID(viewClass, "SYSTEM_UI_FLAG_IMMERSIVE_STICKY", "I");

    // Get flags
    const int flag_SYSTEM_UI_FLAG_LAYOUT_STABLE = env->GetStaticIntField(viewClass, id_SYSTEM_UI_FLAG_LAYOUT_STABLE);
    const int flag_SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = env->GetStaticIntField(viewClass, id_SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
    const int flag_SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = env->GetStaticIntField(viewClass, id_SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    const int flag_SYSTEM_UI_FLAG_HIDE_NAVIGATION = env->GetStaticIntField(viewClass, id_SYSTEM_UI_FLAG_HIDE_NAVIGATION);
    const int flag_SYSTEM_UI_FLAG_FULLSCREEN = env->GetStaticIntField(viewClass, id_SYSTEM_UI_FLAG_FULLSCREEN);
    const int flag_SYSTEM_UI_FLAG_IMMERSIVE_STICKY = env->GetStaticIntField(viewClass, id_SYSTEM_UI_FLAG_IMMERSIVE_STICKY);

    // Get current immersiveness
    const int currentVisibility = env->CallIntMethod(decorViewObj, getSystemUiVisibility);
    const bool is_SYSTEM_UI_FLAG_LAYOUT_STABLE = (currentVisibility & flag_SYSTEM_UI_FLAG_LAYOUT_STABLE) != 0;
    const bool is_SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = (currentVisibility & flag_SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION) != 0;
    const bool is_SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = (currentVisibility & flag_SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN) != 0;
    const bool is_SYSTEM_UI_FLAG_HIDE_NAVIGATION = (currentVisibility & flag_SYSTEM_UI_FLAG_HIDE_NAVIGATION) != 0;
    const bool is_SYSTEM_UI_FLAG_FULLSCREEN = (currentVisibility & flag_SYSTEM_UI_FLAG_FULLSCREEN) != 0;
    const bool is_SYSTEM_UI_FLAG_IMMERSIVE_STICKY = (currentVisibility & flag_SYSTEM_UI_FLAG_IMMERSIVE_STICKY) != 0;

    const auto isAlreadyImmersive =
        is_SYSTEM_UI_FLAG_LAYOUT_STABLE &&
        is_SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION &&
        is_SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN &&
        is_SYSTEM_UI_FLAG_HIDE_NAVIGATION &&
        is_SYSTEM_UI_FLAG_FULLSCREEN &&
        is_SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
    PORTIS_LOGD()
        << "set_immersive data"
        << is_SYSTEM_UI_FLAG_LAYOUT_STABLE
        << is_SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
        << is_SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        << is_SYSTEM_UI_FLAG_HIDE_NAVIGATION
        << is_SYSTEM_UI_FLAG_FULLSCREEN
        << is_SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
    auto success = true;
    if (true) {
        const int flag =
            flag_SYSTEM_UI_FLAG_LAYOUT_STABLE |
            flag_SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
            flag_SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
            flag_SYSTEM_UI_FLAG_HIDE_NAVIGATION |
            flag_SYSTEM_UI_FLAG_FULLSCREEN |
            flag_SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
        env->CallVoidMethod(decorViewObj, setSystemUiVisibility, flag);
        if(env->ExceptionCheck()) {
            // Read exception msg
            jthrowable e = env->ExceptionOccurred();
            env->ExceptionClear(); // clears the exception; e seems to remain valid
            jclass clazz = env->GetObjectClass(e);
            jmethodID getMessage = env->GetMethodID(clazz, "getMessage", "()Ljava/lang/String;");
            jstring message = (jstring)env->CallObjectMethod(e, getMessage);
            const char *mstr = env->GetStringUTFChars(message, NULL);
            const auto exception_msg = std::string{mstr};
            env->ReleaseStringUTFChars(message, mstr);
            env->DeleteLocalRef(message);
            env->DeleteLocalRef(clazz);
            env->DeleteLocalRef(e);
            PORTIS_LOGW() << "set_immersive exception [" << exception_msg << "]";
            success = false;
        }
        else {
            PORTIS_LOGI() << "set_immersive success";
        }
    }
    env->DeleteLocalRef(windowObj);
    env->DeleteLocalRef(decorViewObj);
    return success;
}
于 2018-10-25T15:30:48.687 回答