http://blog.csdn.net/a56573016613/article/details/11011419
在添加Utils类
package com.ndk.test;
import android.widget.Toast;
public class Utils {
public static String staticfun(String info) {
showInfo(info);
return "222";
}
public void info(String info) {
showInfo(info);
}
private static void showInfo(String info) {
Toast t = Toast.makeText(MainActivity.mContext, info,
Toast.LENGTH_SHORT);
t.show();
}
}
在JniClient里面加2个方法
package com.ndk.test;
public class JniClient {
static {
System.loadLibrary("TestNdk");
}
static public native String AddStr(String strA, String strB);
static public native int AddInt(int a, int b);
static public native float distance(Point a, Point b);
static public native void teststaticfun();
static public native void testfun();
}
添加CToJava.c
#include <string.h>
#include <stdio.h>
#include <android/log.h>
#include <jni.h>
/**
* Java 中 声明的native fun 方法的实现
*/
JNIEXPORT void JNICALL Java_com_ndk_test_JniClient_testfun(JNIEnv* env, jobject obj)
{
__android_log_print(ANDROID_LOG_INFO, "CTOJAVA", "fun Begin" );
jclass clazz = (*env)->FindClass(env,"com/ndk/test/Utils"); //通过指定路径获取类的对象
if(clazz == NULL){
__android_log_print(ANDROID_LOG_INFO, "CTOJAVA","clazz not found");
return; //如果类没有找到
}
jmethodID mid = (*env)->GetMethodID(env, clazz, "info", "(Ljava/lang/String;)V"); //查找java中的info方法的ID,最后的签名符号为void类型
if (mid == NULL) {
__android_log_print(ANDROID_LOG_INFO, "CTOJAVA","method testfun ID not found");
return; //如果方法ID没有找到
}
jmethodID construction_id = (*env)->GetMethodID(env, clazz,"<init>", "()V");//"<init>"代表可以访问默认构造函数
if (construction_id == 0) {
__android_log_print(ANDROID_LOG_INFO, "CTOJAVA","construction_id is 0");
}
jobject clazzobject = (*env)->NewObject(env, clazz,construction_id);//创建对象
if(clazzobject == NULL)
{
__android_log_print(ANDROID_LOG_INFO,"CTOJAVA", "clazzobject is not found");
}
__android_log_print(ANDROID_LOG_INFO,"CTOJAVA", "will execute testfun function");
jstring jstrMSG =(*env)->NewStringUTF(env, "Hi,I'm From C 我是非静态方法");
(*env)->CallVoidMethod(env,clazzobject,mid,jstrMSG); //执行info方法
// (*env)->CallVoidMethod(env,clazzobject,mid); //不带参数
}
/**
* Java 中 声明的native staticfun 方法的实现
*/
JNIEXPORT void JNICALL Java_com_ndk_test_JniClient_teststaticfun(JNIEnv* env, jobject obj)
{
__android_log_print(ANDROID_LOG_INFO, "CTOJAVA", "staticfun");
jclass clazz = (*env)->FindClass(env, "com/ndk/test/Utils"); //通过指定路径获取类的对象
if (clazz == NULL ) {
__android_log_print(ANDROID_LOG_INFO, "CTOJAVA", "clazz not found");
return; //如果类没有找到
}
jmethodID mid = (*env)->GetStaticMethodID(env, clazz, "staticfun", "(Ljava/lang/String;)Ljava/lang/String;"); //查找java中的staticfun方法的ID,最后的签名符号为void类型
if (mid == NULL ) {
__android_log_print(ANDROID_LOG_INFO, "CTOJAVA","method staticfun ID not found");
return; //如果方法ID没有找到
}
__android_log_print(ANDROID_LOG_INFO, "CTOJAVA",
"will execute testfun function");
jstring jstrMSG = (*env)->NewStringUTF(env, "Hi,I'm From C 我是静态方法");
jstring jstr = (*env)->CallStaticObjectMethod(env, clazz,mid,jstrMSG);
jstring cstr = (char*) (*env)->GetStringUTFChars(env,jstr, 0);//这个在下面源码中没转换 注意一下
__android_log_print(ANDROID_LOG_INFO, "JNIMsg", "Success Value = %s",cstr );
// (*jniEnv)->ReleaseStringUTFChars(jniEnv, jstr, cstr);
// (*jniEnv)->DeleteLocalRef(jniEnv, jstr);
}
Java 方法映射到C中的签名
http://www.linuxidc.com/wap.aspx?nid=77231&cid=6
最后是源码http://download.csdn.net/detail/a56573016613/6512605