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

由于无法从清单(Set Android immersive full screen mode in manifest)进行设置,是否可以通过EGL请求它?

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

最佳答案

回答我自己的问题,您可以通过C ++ / JNI设置沉浸模式,而无需在项目中添加Java。这是我的摘录,很大一部分是从Internet上的某个地方复制的。

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;
}

10-01 05:35