Skip to content

Instantly share code, notes, and snippets.

@andr1972
Created September 7, 2013 08:06
Show Gist options
  • Save andr1972/6473786 to your computer and use it in GitHub Desktop.
Save andr1972/6473786 to your computer and use it in GitHub Desktop.
Process.cpp
#include <jni.h>
#include "demopkg_Process.h"
JNIEXPORT void JNICALL Java_demopkg_Process_nativeLongOperation
(JNIEnv *env, jobject owner, jobject param1)
{
jclass clsICallback = env->GetObjectClass(param1);
jmethodID method1 = env->GetMethodID(clsICallback, "onPercent", "(I)V");
jmethodID method2 = env->GetMethodID(clsICallback, "onPercentEx", "(Ldemopkg/ComplexStruct;)V");
jclass clsComplexStruct = env->FindClass("demopkg/ComplexStruct");
jbyte bytes[10];
for (int i=1; i<=10; i++) bytes[i-1] = i;
char str[32];
jobject priorObj = NULL;
for (int i=1; i<=10; i++) {
env->CallVoidMethod(param1, method1, i);
jobject obj = env->AllocObject(clsComplexStruct);
jfieldID fld = env->GetFieldID(clsComplexStruct, "fieldI","I");
env->SetIntField(obj, fld, i);
fld = env->GetFieldID(clsComplexStruct, "arr","[B");
jbyteArray byteArray = env->NewByteArray(i);
env->SetByteArrayRegion(byteArray, 0, i, bytes);
env->SetObjectField(obj, fld, byteArray);
fld = env->GetFieldID(clsComplexStruct, "str","Ljava/lang/String;");
sprintf(str,"a%d", i);
env->SetObjectField(obj, fld, env->NewStringUTF(str));
fld = env->GetFieldID(clsComplexStruct, "prior","Ldemopkg/ComplexStruct;");
env->SetObjectField(obj, fld, priorObj);
env->CallVoidMethod(param1, method2, obj);
priorObj = obj;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment