JNI實現回撥| JNI呼叫JAVA函式|引數和返回值的格式
阿新 • • 發佈:2018-12-31
程式碼下載:http://dl.dbank.com/c0c0xs3b24
一、JNI實現回撥
通過JNI在Native層呼叫Java層的方法,來實現Native層向JAVA層傳遞訊息。
JNICallback.java
- publicclass JNICallback extends Activity {
- static {
- System.loadLibrary("callback");
- }
- publicvoid callback(int notify_id, int param)
- {
- Log.e("JNICallback",
- }
- publicnativevoid setCallBack();
- /** Called when the activity is first created. */
- @Override
- publicvoid 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"
- 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