JAVA 代码
1.NativeProviefder.java
package com.example.test;
public class NativeProviefder {
static {
System.loadLibrary("test_jni");
}
public native void nativeInitilize();
public native void start();
public native void stop();
public void onNativeCallback(int count) {
System.out.println("onNativeCallback count=" + count);
}
}
2.MainActivity.java
package com.example.test;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
NativeProviefder np = new NativeProviefder();
boolean isstart = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
np.nativeInitilize();
((Button) findViewById(R.id.btnstart)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (isstart) {
np.stop();
} else {
np.start();
}
}
});
}
...
}
JNI代码
test_jni.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <string.h>
#include <android/log.h>
#include <jni.h>
#include <pthread.h>
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "", __VA_ARGS__));
JavaVM* gJavaVM;
jobject gJavaObj;
static volatile int gExit = 0;
static void* native_thread_exec(void* arg) {
JNIEnv *env;
int status;
LOGI("onNativeCallback thread1");
status = (*gJavaVM)->AttachCurrentThread(gJavaVM, &env, NULL);
if (status < 0) {
LOGI("onNativeCallback modem_log, Failed to GetObjectClass");
return 0;
}
LOGI("onNativeCallback thread2");
jclass javaClass = (*env)->GetObjectClass(env, gJavaObj);
if (javaClass == NULL) {
LOGI("onNativeCallback Failed to find javaClass");
return 0;
}
LOGI("onNativeCallback thread3 javaClass (I)V");
jmethodID javaCallback = (*env)->GetMethodID(env, javaClass, "onNativeCallback", "(I)V"); //这一句报错,提示:java.lang.NoSuchMethodError: no non-static method "Lcom/example/test/NativeProviefder;.onNativeCallback(I)V"
if (javaCallback == NULL) {
LOGI("onNativeCallback modem_log, Failed to find method javaCallback");
(*gJavaVM)->DetachCurrentThread(gJavaVM);
return 0;
}
int count = 0;
while (!gExit) {
(*env)->CallVoidMethod(env, gJavaObj, javaCallback, count++);
sleep(1);
}
(*gJavaVM)->DetachCurrentThread(gJavaVM);
return 1;
}
JNIEXPORT void JNICALL Java_com_example_test_NativeProviefder_nativeInitilize(JNIEnv * env, jobject thiz) {
(*env)->GetJavaVM(env, &gJavaVM);
gJavaObj = (*env)->NewGlobalRef(env, thiz);
}
JNIEXPORT void JNICALL Java_com_example_test_NativeProviefder_start(JNIEnv * env, jobject thiz) {
pthread_t threadId;
if (pthread_create(&threadId, NULL, native_thread_exec, NULL) != 0) {
LOGI("onNativeCallback start faild!");
return;
}
LOGI("onNativeCallback start success");
}
JNIEXPORT void JNICALL Java_com_example_test_NativeProviefder_stop(JNIEnv * env, jobject thiz) {
gExit = 1;
LOGI("onNativeCallback stop!");
}
Jni的Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := test_jni.c
LOCAL_C_INCLUDES := $(JNI_H_INCLUDE) $(LOCAL_PATH) $(MTK_ROOT)/external/meta/common/inc
LOCAL_MODULE := libtest_jni
LOCAL_SHARED_LIBRARIES := libutils libcutils
#LOCAL_PRELINK_MODULE := true
LOCAL_MODULE_TAGS := optional eng
LOCAL_PROGUARD_ENABLED:= disabled
include $(BUILD_SHARED_LIBRARY)
错误找不到JAVA里的函数,不知道什么原因,大神们,请指教?
jmethodID javaCallback = (*env)->GetMethodID(env, javaClass, "onNativeCallback", "(I)V"); //这一句报错,提示:java.lang.NoSuchMethodError: no non-static method "Lcom/example/test/NativeProviefder;.onNativeCallback(I)V"
补充:是在android5.1上运行。