Android alert display functionality using JNI

This commit is contained in:
saschawillems 2018-04-30 22:24:45 +02:00
parent 1227f1e7f4
commit 5056563f5d
5 changed files with 64 additions and 3 deletions

View file

@ -11,6 +11,7 @@
#if defined(__ANDROID__)
#include <android/log.h>
#include <dlfcn.h>
#include <android/native_window_jni.h>
android_app* androidApp;
@ -292,6 +293,23 @@ namespace vks
vks::android::screenDensity = AConfiguration_getDensity(config);
AConfiguration_delete(config);
}
// Displays a native alert dialog using JNI
void showAlert(const char* message) {
JNIEnv* jni;
androidApp->activity->vm->AttachCurrentThread(&jni, NULL);
jstring jmessage = jni->NewStringUTF(message);
jclass clazz = jni->GetObjectClass(androidApp->activity->clazz);
// Signature has to match java implementation (arguments)
jmethodID methodID = jni->GetMethodID(clazz, "showAlert", "(Ljava/lang/String;)V");
jni->CallVoidMethod(androidApp->activity->clazz, methodID, jmessage);
jni->DeleteLocalRef(jmessage);
androidApp->activity->vm->DetachCurrentThread();
return;
}
}
}

View file

@ -26,6 +26,7 @@
#include <android_native_app_glue.h>
#include <android/configuration.h>
#include <memory>
#include <string>
// Missing from the NDK
namespace std
@ -169,6 +170,7 @@ namespace vks
void loadVulkanFunctions(VkInstance instance);
void freeVulkanLibrary();
void getDeviceConfig();
void showAlert(const char* message);
}
}

View file

@ -268,8 +268,9 @@ namespace vks
if (!errorModeSilent) {
MessageBox(NULL, message.c_str(), NULL, MB_OK | MB_ICONERROR);
}
#elif defined(__ANDROID__)
LOGE("Fatal error: %s", message.c_str());
#elif defined(__ANDROID__)
LOGE("Fatal error: %s", message.c_str());
vks::android::showAlert(message.c_str());
#endif
std::cerr << message << "\n";
exit(exitCode);