J'ai un Android classe Java:Android appel démon natif de méthode Java via JNI
package com.test.Testing;
public class MainActivity extends Activity {
static {
System.loadLibrary("testcontrol");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void tryBack() {
Log.v("test", "callback called");
}
}
je aussi, une bibliothèque native partagée appelée « libtestcontrol » avec JNI_OnLoad et une méthode que je suis en train d'utiliser pour appeler la méthode Java tryBack:
JavaVM * global_vm;
jint JNI_OnLoad(JavaVM* vm, void* reserved) {
JNIEnv jvm_env;
if ((*vm)->GetEnv(vm, (void **) &jvm_env, JNI_VERSION_1_6) != JNI_OK) {
alog_error ("JNI_ONLOAD Failed to get the environment using GetEnv()");
return -1;
}
// *** Supposed to cache vm_global. I've tried too vm_global=vm ****
(*jvm_env)->GetJavaVM(jvm_env, &vm_global);
if (vm_global == NULL) {
alog_error ("JNI_ONLOAD: vm_global is NULL");
}
else {
alog_error ("JNI_ONLOAD: vm_global is NOT NULL <- by here");
}
return JNI_VERSION_1_6;
}
void makeCallto_tryBack(){
if (vm_global == NULL)
alog_error ("MAKECALL: vm_global is NULL <- by here");
else {
alog_error ("MAKECALL: vm_global is NOT NULL");
if ((*vm_global)->AttachCurrentThreadAsDaemon(vm_global, &jvm_env, NULL) != 0) {
alog_error ("JNI Failed to attach thread as daemon");
//
// Rest of code for execute Java call to tryCallback
//
// if ((*vm_global)->GetEnv(vm_global, (void**) &jvm_env, JNI_VERSION_1_6) != JNI_OK) {
// alog_error ("Failed to get the environment using GetEnv()");
// }
// (....)
}
}
enfin, j'ai un démon autonome C inited à init.rc qui partage la bibliothèque « libtestcontrol » et après un certain temps appelle à makeCallto_tryBack méthode de bibliothèque partagée.
void periodicProccess(){
makeCallto_tryBack();
// (...)
}
Qu'advient-il alors, est que la référence "vm_global" est toujours NULL dans la méthode makeCalltoTryCallback(), est barbante inited pas correctement nul dans JNI_Onload. En d'autres termes, il n'y a pas de persistance de la variable JavaVM vm_global après JNI_OnLoad. Comment puis-je resoudre ceci?
Merci Alex. Je pensais ainsi, mais a été confondu avec les spécifications JNI. Je vais vérifier une méthode pour partager des données entre les processus. – jlcarretero