一、JNI实现回调
通过JNI在Native层调用JAVA层的方法,来实现Native层向JAVA层传递消息。
JNICallback.java
- public class JNICallback extends Activity {
- static {
- System.loadLibrary("callback");
- }
- public void callback(int notify_id, int param)
- {
- Log.e("JNICallback","notify_id="+notify_id+";param="+param);
- }
- public native void setCallBack();
-
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- setCallBack();
- }
com_JNICallback_JNICallback.cpp
- void JNICALL Java_com_JNICallback_JNICallback_setCallBack
- (JNIEnv *env, jobject obj)
- {
- LOGE("jni callback (0)");
- jclass cls = env->GetObjectClass(obj);
- jmethodID callback = env->GetMethodID(cls,"callback","(II)V");
- env->CallVoidMethod(obj,callback,5,10);
- LOGE("jni callback (1)");
- }
javah -classpath . -jni com.JNICallback.JNICallback(包名+类名)
二、JNI调用JAVA函数
获取实例的类定义 jclass cls = env->GetObjectClass(obj);
获取方法定义 jmethodID callback = env->GetMethodID(cls,"callback","(II)V");
调用方法 env->CallVoidMethod(obj,callback,5,10)
三、参数和返回值的格式
参考 http://java.sun.com/docs/books/jni/html/jniTOC.html
(stefzeus) |