4 Use your own C++ class to call function defined in the xml file¶
Please follow these steps to call functions defined in xml file.
- Ensure “AndroidJNI.h” and “AndroidApplication.h”to be included in the cpp file.
- Get Java runtime environment by using FAndroidApplication::GetJavaEnv().
- Use JavaWrapper::FindMethod() to obtain ID of the java function defined in xml file.
- For example, use FJavaWrapper::CallBooleanMethod() to invoke Android API which returns boolean value. For other different definitions, you need use corresponding FJavaWrapper methods.
Please refer to the following code:
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyBlueprintJarTest.h"
#if PLATFORM_ANDROID
#include "Android/AndroidApplication.h"
#include "Android/AndroidJNI.h"
#include <android/log.h>
#endif
void UMyBlueprintJarTest::TestLED(int led, int color, int blink, int ontime, int offtime)
{
#if PLATFORM_ANDROID
if (JNIEnv* Env = FAndroidApplication::GetJavaEnv())
{
static jmethodID Method = FJavaWrapper::FindMethod(Env, FJavaWrapper::GameActivityClassID, "AndroidThunkJava_UEPvr_SetLED", "(IIIII)Z", false);
FJavaWrapper::CallBooleanMethod(Env, FJavaWrapper::GameActivityThis, Method, led, color, blink, ontime, offtime);
}
#endif
}