代码下载:http://dl.dbank.com/c0c0xs3b24
一、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();
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- setCallBack();
- }
- <span style="font-family:Comic Sans MS;">}</span>
- 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
转自:http://blog.csdn.net/stefzeus/article/details/6721346#t1