JNI and the cancellation of AsyncTask… here is a solution to the problem.
JNI and the cancellation of AsyncTask
I
have an AsyncTask and in doInBackground() I call the C function and it executes for a while. When I cancel my AsyncTask by calling cancel(true), it returns true – like the task was terminated, but I still see log statements from C code after “terminating”.
To me, the C function doesn’t seem to really terminate.
My question is – how do I properly terminate the execution of a C function?
Solution
Find the answer here .
This is version C:
jclass thread = (*env)->FindClass(env, "java/lang/Thread");
jmethodID mCurThread = (*env)->GetStaticMethodID(env, thread, "currentThread", "()Ljava/lang/Thread;" );
jmethodID mIsInterrupted = (*env)->GetMethodID(env, thread, "isInterrupted", "()Z");
jobject curThread = (jobject)(*env)->CallStaticObjectMethod(env, thread, mCurThread);
for (; ;) {
do some stuff
jboolean res = (jboolean)(*env)->CallBooleanMethod(env, curThread, mIsInterrupted);
if (res == JNI_TRUE) {
LOGI("INTERRUPTED");
} else {
LOGI("WORKING");
}
}