• c++回调java方法


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

    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代码如果写多了,也许会不太适应。。。。

  • 相关阅读:
    设计模式学习——前言和目录
    模板颜色搭配
    win7、xp下Meclipse SVN用户名修改
    JS编码解码
    用Javascript进行HTML转义(分享)
    打印异常信息
    lucene 抛出的异常(分享)
    SQL语句优化(分享)
    Java集群之session共享解决方案
    VUE中返回上一页
  • 原文地址:https://www.cnblogs.com/budaixi/p/4058378.html
Copyright © 2020-2023  润新知