Android NDK Call Java From C++
2013-01-26 23:55
357 查看
I search the Android NDK document and Internet a bit, and could not find anything useful to help me play sound with C++ under the Android NDK. Most of topics are about how to play sound with Java, so I grape an idea that why not call Java from C++. I tried along this way, and finally all those stuff works as I expected before.
Function JNI_OnLoad and JNI_OnUnload are Android NDK system call back functions, that will allow us to get the current Java virtual machine. We need to get the class static method or object member function from the Java virtual machine. Now we could call function SysPlaySound as a C/C++ native function.
/article/5095940.html
http://qfqf16.blog.163.com/blog/static/128109527201281263955386/
http://blog.chinaunix.net/uid-7448773-id-310170.html
Implement Java static function
A Sound player class implemented in Java class. In addition to play sound basic feature, I also make it expose a static function that will be called from C++. The reason why I used a static function here is that static function could allow me correctly find the right Java object instance, the one with sound pool initialized well and file loaded ok will be the correct one.public class SoundManager { ...... public static void sPlaySound(int sound) { if ( null != sInstance ) sInstance.PlaySound(sound); } }
Call Java function from C/C++
static JavaVM* g_JavaVM = NULL; static const char *g_JavaClassName = "com/easygame/SoundManager"; JNIEXPORT jint JNI_OnLoad(JavaVM* jvm, void* reserved) { g_JavaVM = jvm; JNIEnv *env = NULL; if (jvm->GetEnv((void**) &env, JNI_VERSION_1_6) != JNI_OK) return -1; return JNI_VERSION_1_6; } JNIEXPORT void JNI_OnUnload(JavaVM* jvm, void* reserved) { g_JavaVM = NULL; JNIEnv *env = NULL; if (jvm->GetEnv((void**) &env, JNI_VERSION_1_6) != JNI_OK) return; } void SysPlaySound(int soundId) { if (g_JavaVM == NULL) return; int status = -1; JNIEnv *env = NULL; bool isAttached = false; status = g_JavaVM->GetEnv((void**) &env, JNI_VERSION_1_6); if (status < 0) { status = g_JavaVM->AttachCurrentThread(&env, NULL); if (status < 0) return; isAttached = true; } if ( env != NULL ) { jclass cls = env->FindClass(g_JavaClassName); if ( cls != 0 ) { jmethodID mid = env->GetStaticMethodID(cls, "sPlaySound", "(I)V"); if ( mid != 0 ) env->CallStaticVoidMethod(cls, mid, soundId); } } if ( isAttached ) g_JavaVM->DetachCurrentThread(); }
Function JNI_OnLoad and JNI_OnUnload are Android NDK system call back functions, that will allow us to get the current Java virtual machine. We need to get the class static method or object member function from the Java virtual machine. Now we could call function SysPlaySound as a C/C++ native function.
Reference
/article/2085545.html/article/5095940.html
http://qfqf16.blog.163.com/blog/static/128109527201281263955386/
http://blog.chinaunix.net/uid-7448773-id-310170.html
相关文章推荐
- Android - How can I call Java methods from C++
- CC++初学者编程教程(10) 搭建Android java C/C++ NDK QTforAndroid 开发环境
- Android-NDK开发之第四个例子--用C/C++调用Java
- android ndk c/c++ 调用java
- CC++初学者编程教程(10) 搭建Android java C/C++ NDK QTforAndroid 开发环境
- android jni c/c++线程通过CallVoidMethod调用java函数出现奔溃问题
- Linux环境下android平台调试native代码, 从java debug 到C++ (NDK DEBUG)
- Linux环境下android平台调试native代码, 从java debug 到C++ (NDK DEBUG)
- 【Android性能优化】使用NDK进行Java和C++混编
- 转战Android之NDK(jni, c/c++调用java)
- JNI NDK (AndroidStudio+CMake )实现C C++调用Java代码流程
- Windows环境下android平台native调试,从java debug 到 C++ (NDK DEBUG)
- android ubuntu jni so 报错Java_com_gcx_ndkjni_HelloNdkJni_stringFromNDKJNI
- How to call a android/native service from a native/android (java) code
- Android Studio NDK 入门教程(3)--Java与C++之间的类型签名
- Linux环境下android平台调试native代码, 从java debug 到C++ (NDK DEBUG)
- Android-NDK开发之基础--Android JNI实例代码(一)-- 在JNI中执行Java方法--C/C++调用Java
- Android NDK中C++调用Java
- call Java methods from C++
- Android JNI/NDK开发(2)JNI实现C/C++与Android/JAVA相互调用