diff --git a/src/wakelock.c b/src/wakelock.c index 54e29a76..efb14d87 100644 --- a/src/wakelock.c +++ b/src/wakelock.c @@ -29,7 +29,6 @@ void belle_sip_wake_lock_init(JNIEnv *env, jobject pm) { jfieldID fieldID; (*env)->GetJavaVM(env, &ctx.jvm); - ctx.powerManager = (*env)->NewGlobalRef(env, pm); pthread_key_create(&ctx.jniEnvKey, jni_key_cleanup); powerManagerClass = (*env)->FindClass(env, "android/os/PowerManager"); @@ -45,10 +44,12 @@ void belle_sip_wake_lock_init(JNIEnv *env, jobject pm) { } else { belle_sip_warning("bellesip_wake_lock_init(): the wakelock system has already been initialized"); } + if (ctx.powerManager == NULL) { + ctx.powerManager = (*env)->NewGlobalRef(env, pm); + } } void belle_sip_wake_lock_uninit(JNIEnv *env) { - ctx.jvm = NULL; if(ctx.powerManager != NULL) { (*env)->DeleteGlobalRef(env, ctx.powerManager); ctx.powerManager = NULL; @@ -61,7 +62,7 @@ void belle_sip_wake_lock_uninit(JNIEnv *env) { * @param data Unused. */ static void jni_key_cleanup(void *data) { - JNIEnv *env = pthread_getspecific(ctx.jniEnvKey); + JNIEnv *env = (JNIEnv*) data; belle_sip_message("Thread end. Cleanup wake lock jni environment"); if(env != NULL) { if(ctx.jvm != NULL) {