码迷,mamicode.com
首页 > 编程语言 > 详细

c++回调java方法

时间:2014-10-29 01:54:04      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   ar   使用   java   sp   div   

还是直接上代码再来解释吧。。。。

static JavaVM* g_JavaVM = NULL;
static jobject g_InterfaceObject = 0;
static const char *g_JavaClassName = "xxx/xxx/xxxx/Provider";

static void GetInterfaceObject(JNIEnv *env, const char *path, jobject *objptr)
{
    jclass cls = env->FindClass(path);
    if (!cls)
    {
        return;
    }
    jmethodID constr = env->GetMethodID(cls, "<init>", "()V");
    if (!constr)
    {
        return;
    }
    jobject obj = env->NewObject(cls, constr);
    if (!obj)
    {
        return;
    }
    (*objptr) = env->NewGlobalRef(obj);
}

jint JNI_OnLoad(JavaVM *vm, void *reserved)
{
    g_JavaVM = vm;

    JNIEnv *env;
    if (vm->GetEnv((void**) &env, JNI_VERSION_1_6) != JNI_OK)
    {
        return -1;
    }

    GetInterfaceObject(env, g_JavaClassName, &g_InterfaceObject);

    return JNI_VERSION_1_6;

}

static jobject getInstance(JNIEnv *env, jclass obj_class)
{
    jmethodID  c_id = env->GetMethodID(obj_class, "<init>", "()V");
    jobject obj = env->NewObject(obj_class, c_id);
    return obj;
}

void ConnectSuccessNotice(const int nWidth, const int nHeight, const int nFps)
{
    if (g_JavaVM == NULL)
    {
        return;
    }
    int status;
    JNIEnv *env = NULL;
    bool isAttached = false;

    status = g_JavaVM->GetEnv((void**) &env, JNI_VERSION_1_6);

    if (status < 0)  //
   {
        status = g_JavaVM->AttachCurrentThread(&env, NULL);
        if (status < 0)
        {
            return;
        }
        isAttached = true;
    }

    if (isAttached)  // 
    {
        jclass cls = env->GetObjectClass(g_InterfaceObject);
        if (cls != 0)
        {
            jmethodID mid = env->GetStaticMethodID(cls, "ConnectSuccessNoticeJavaThread", "(III)V");
            if (mid != 0)
            {
                env->CallStaticVoidMethod(cls, mid, nWidth, nHeight, nFps);
            }
        }
        g_JavaVM->DetachCurrentThread();
    }
    else  // 
    {
        jclass cls = env->FindClass(g_JavaClassName);
        if (cls != 0)
        {

            jobject obj = getInstance(env, cls);
            jmethodID mid = env->GetMethodID(cls, "ConnectSuccessNoticeJava", "(III)V");
            if (mid != 0)
            {
                env->CallVoidMethod(obj, mid, nWidth, nHeight, nFps);
            }
        }
    }

}


void OnDisconnectedNotice()
{
    if (g_JavaVM == NULL)
    {
        return;
    }
    int status;
    JNIEnv *env = NULL;
    bool isAttached = false;

    status = g_JavaVM->GetEnv((void**) &env, JNI_VERSION_1_6);


    if (status < 0)
   {
        status = g_JavaVM->AttachCurrentThread(&env, NULL);
        if (status < 0)
        {
            return;
        }
        isAttached = true;
    }

    if (isAttached)  // 
    {
        jclass cls = env->GetObjectClass(g_InterfaceObject);
        if (cls != 0)
        {
            jmethodID mid = env->GetStaticMethodID(cls, "OnDisconnectedJavaThread", "()V");
            if (mid != 0)
            {
                env->CallStaticVoidMethod(cls, mid);
            }
        }
        g_JavaVM->DetachCurrentThread();
    }
    else  // 
    {
        jclass cls = env->FindClass(g_JavaClassName);
        if (cls != 0)
        {

            jobject obj = getInstance(env, cls);
            jmethodID mid = env->GetMethodID(cls, "OnDisconnectedJava", "()V");
            if (mid != 0)
            {
                env->CallVoidMethod(obj, mid);
            }
        }
    }

}

void ReceiveDataCallback(const char *nbuff,const int nLength,const int nKey, const int nTimeStamp, string sDeviceID)
{

    if (g_JavaVM == NULL)
    {
        return;
    }
    int status;
    JNIEnv *env = NULL;
    bool isAttached = false;

    status = g_JavaVM->GetEnv((void**) &env, JNI_VERSION_1_6);

    if (status < 0)  //
   {
        status = g_JavaVM->AttachCurrentThread(&env, NULL);
        if (status < 0)
        {
            return;
        }
        isAttached = true;
    }

    if (isAttached)  
    {

        jclass cls = env->GetObjectClass(g_InterfaceObject);
        if (cls != 0)
        {
            jmethodID mid = env->GetStaticMethodID(cls, "ReceiveDataCallbackJavaThread", "(Ljava/lang/String;III[B)V");
            if (mid != 0)
            {
                jbyteArray jBufferArray = env->NewByteArray(nLength);
                jbyte *jby =env->GetByteArrayElements(jBufferArray, 0);

                memcpy(jby, nbuff, nLength);

                env->SetByteArrayRegion(jBufferArray, 0, nLength, jby);

                jstring jsDeviceID = env->NewStringUTF(sDeviceID.c_str());

                env->CallStaticVoidMethod(cls, mid,jsDeviceID, nLength, nKey, nTimeStamp,jBufferArray);

                env->ReleaseByteArrayElements(jBufferArray,jby,0);

                env->DeleteLocalRef(jBufferArray);
            }
        }
        g_JavaVM->DetachCurrentThread();
    }
    else  // PrintNdkLog不是在线程中被调用, 调用Java类成员API
    {

        jclass cls = env->FindClass(g_JavaClassName);
        if (cls != 0)
        {

            jobject obj = getInstance(env, cls);
            jmethodID mid = env->GetMethodID(cls, "ReceiveDataCallbackJava", "(Ljava/lang/String;III[B)V");
            if (mid != 0)
            {
                jbyteArray jBufferArray = env->NewByteArray(nLength);

                jbyte *jby =env->GetByteArrayElements(jBufferArray, 0);

                memcpy(jby, nbuff, nLength);

                env->SetByteArrayRegion(jBufferArray, 0, nLength, jby);

                jstring jsDeviceID = env->NewStringUTF(sDeviceID.c_str());

                env->CallVoidMethod(obj, mid,jsDeviceID, nLength, nKey, nTimeStamp,jBufferArray);

                env->ReleaseByteArrayElements(jBufferArray,jby,0);

                env->DeleteLocalRef(jBufferArray);
            }
        }
    }
}

唯一需要解释的只有两个地方

第一个就是在NDK中创建的线程中, 只允许调用静态的Java API,只能使CallStaticVoidMethod

还有一个就是JNI_OnLoad,在这里把JavaVM指针保存下来

重新改方法时,java会先调用该组件的JNI_OnLoad()函数.其用途有二: 

一是:告诉java VM此C组件使用那一个JNI版本。如果你没有提供JNI_OnLoad()函数,VM会默认使用最老的JNI 1.1版本。由于新版的JNI做了许多扩充,如果需要使用JNI的新版功能,例如JNI 1.4的java.nio.ByteBuffer,就必须藉由JNI_OnLoad()函数来告知VM。

二是:由于VM执行到System.loadLibrary()函数时,就会立即先呼叫JNI_OnLoad(),所以C组件的开发者可以藉由JNI_OnLoad()来进行C组件内的初期值之设定,也就是获取JavaVM接口。

唯一需要多注意的就是内存泄露的问题,java代码如果写多了,也许会不太适应。。。。

c++回调java方法

标签:style   blog   io   color   ar   使用   java   sp   div   

原文地址:http://www.cnblogs.com/budaixi/p/4058378.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!