Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

android call custom managed method from native code

JNIEnv *env = activity->env;
jobject activity_obj = activity->clazz;

// Custom classes cannot be found directly - such a class needs to be loaded with Activity's ClassLoader.

// Get ClassLoader:
jclass activity_class = env->GetObjectClass(activity_obj);
jmethodID get_class_loader_mid = end->GetMethodID(activity_class, "getClassLoader", "()Ljava/lang/ClassLoader;");
jobject class_loader_obj = env->CallObjectMethod(activity_obj, get_class_loader_mid);

// Use the ClassLoader to load the custom class:
jclass class_loader_class = env->FindClass("java/lang/ClassLoader");
jmethodID find_class_mid = env->GetMethodID(class_loader_class, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");
jstring custom_class_name = env->NewStringUTF("fully.qualified.name.of.CustomClass");
jclass custom_class = reinterpret_cast<jclass>(env->CallObjectMethod(class_loader_obj, find_class_mid, custom_class_name));
env->DeleteLocalRef(custom_class_name);

// Now custom_class can be passed to various 'Call<...>Method' calls.
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #android #call #custom #managed #method #native #code
ADD COMMENT
Topic
Name
8+1 =