zoukankan      html  css  js  c++  java
  • andorid ndk 各种坑啊 记录下

    android jni代码回调java的问题
    因为多线程原因会导致找不到java类,无法call函数的问题
    问题1找不到java类
    在JNI_OnLoad的时候 保存下来

    JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved)
    {
        g_vm = vm;
        JNIEnv* env = NULL;
        jint result = -1;
        if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
            return -1;
        }
        assert(env != NULL);
    
        register_location_methods(env);
        result = JNI_VERSION_1_4;
        return result;
    }
    int register_location_methods(JNIEnv *env)
    {
        jniEnv = env;
        jclass clazz;
        clazz = env->FindClass("com/TongBan/Chat/NetBilling");
        if (clazz == NULL) {
            return -1;
        }
        JNetBilling = clazz;
    
        onReceivedMsgType = env->GetStaticMethodID(clazz,
                        "OnReceivedMsgType", "(I)V");
    //    env->CallStaticVoidMethod(JNetBilling, onReceivedMsgType, 1);
        return 0;
    }

    问题2多线程回调call函数
    此处生成的JNIEnv无法获取到class 函数 等 须要之前保存好的全局变量

    bool isAttacked = false;
        JNIEnv* env;
        int status = g_vm->GetEnv((void **) &env, JNI_VERSION_1_4);
        if (status < 0) {
            status = g_vm->AttachCurrentThread(&env, NULL);
            if (status < 0) {
                return len;
            }
            isAttacked = true;
        }
        env->CallStaticVoidMethod(JNetBilling,onReceivedMsgType,iMsgType);
        if (isAttacked) {
            g_vm->DetachCurrentThread();
        }

    输出问题因为还是比較喜欢cout就百度了个cout输出到logCat日志的

    int iMsgType = MSG_PACKET::GetMsgType(kBuffer);
    
        StreamBuf g_StreamBuf;
        std::cout.rdbuf(&g_StreamBuf);
        std::cout << iMsgType;//就可以显示到logCat
    #include <iostream>
    #include <streambuf>
    
    class StreamBuf : public std::streambuf
    {
        enum
        {
            BUFFER_SIZE = 255,
        };
    
    public:
        StreamBuf()
        {
            buffer_[BUFFER_SIZE] = '';
            setp(buffer_, buffer_ + BUFFER_SIZE - 1);
        }
    
        ~StreamBuf()
        {
            sync();
        }
    
    protected:
        virtual int_type overflow(int_type c)
        {
            if (c != EOF)
            {
                *pptr() = c;
                pbump(1);
            }
            flush_buffer();
            return c;
        }
    
        virtual int sync()
        {
            flush_buffer();
            return 0;
        }
    
    private:
        int flush_buffer()
        {
            int len = int(pptr() - pbase());
            if (len <= 0)
                return 0;
    
            if (len <= BUFFER_SIZE)
                buffer_[len] = '';
    
    #ifdef ANDROID
            android_LogPriority t = ANDROID_LOG_INFO;
            __android_log_write(t, "JNI_DEBUG", buffer_);
    #else
            printf("%s", buffer_);
    #endif
    
            pbump(-len);
            return len;
        }
    
    private:
        char buffer_[BUFFER_SIZE + 1];
    };
    

    到这里 最终能够 互动了 android->server->android

  • 相关阅读:
    线程的中断
    线程间的协作机制
    iOS app内打开safari完成google的OAuth2认证
    iOS ipa 重签名 resign
    iOS rebuild from bitcode对ipa大小的影响
    iOS URL Cache文章推荐 (待完成)
    iOS 推荐几篇关于Objective-c 动态语言的文章
    iOS Code Sign On Copy
    设计模式好文章汇总(不断更新中)
    Json 文件中value的基本类型
  • 原文地址:https://www.cnblogs.com/tlnshuju/p/7399763.html
Copyright © 2011-2022 走看看