美文网首页NDK
NDK开发-3回调 返回java实例

NDK开发-3回调 返回java实例

作者: proud2008 | 来源:发表于2020-03-02 16:38 被阅读0次

    native 实例化java类

    public class Dog {
        public String name;
        public int age;
    
        public Dog(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public Dog(String name) {
            this.name = name;
        }
    
        @Override
        public String toString() {
            StringBuilder field2 = new StringBuilder();
            field2.append("[name: " + name);
            field2.append(";age: " + age + "]");
            return field2.toString();
        }
    
    
    }
    ...
     public static native Dog getDog(String string);
      public static native Dog getDog2(String string,int t);
       log("getDog "+JniData.getDog("11"));
       log("getDog "+JniData.getDog2("22",3));
    

    与上相似只是方法名用"<init>"

    extern "C"
    JNIEXPORT jobject JNICALL
    Java_com_hrg_gys_clfr_myapplication_JniData_getDog(JNIEnv *env, jclass clazz, jstring string) {
        jclass jclass1 = env->FindClass("com/hrg/gys/clfr/myapplication/Dog"); //前没有L不以;号结束
        /* public Dog(String name)*/
        /*<init>构造方法不要写Dog了*/
        //    jmethodID jmethodId = env->GetMethodID(jclass1, "<init>", "(Ljava/lang/String;)V");
        //    jobject jobject1 = env->NewObject(jclass1, jmethodId, string);
        /*调用  public Dog(String name, int age) 构造方法*/
        jmethodID jmethodId = env->GetMethodID(jclass1, "<init>", "(Ljava/lang/String;I)V");
        jobject jobject1 = env->NewObject(jclass1, jmethodId, string, 2);
        return jobject1;
    }
    
    extern "C"
    JNIEXPORT jobject JNICALL
    Java_com_hrg_gys_clfr_myapplication_JniData_getDog2(JNIEnv *env, jclass clazz, jstring string,
                                                        jint t) {
        // TODO: implement getDog2()
        jclass jclass1 = env->FindClass("com/hrg/gys/clfr/myapplication/Dog"); //前没有L不以;号结束
        jmethodID jmethodId = env->GetMethodID(jclass1, "<init>", "(Ljava/lang/String;I)V");
     /*创建类 再调用类的构造方法*/
        jobject jobject1 = env->AllocObject(jclass1);
      /*调用了类的构造方法*/
        env->CallNonvirtualVoidMethod(jobject1,jclass1,jmethodId,string,t);
        return jobject1;
    }
    

    等待native的加调

    public class JniInvokeMethod {
        static {
            System.loadLibrary("native-lib");
        }
        public native void  nativeCallback(CallBack callBack);
        public native void  nativeThreadCallback(CallBack callBack);
    
        public interface  CallBack{
            void doBack();
        }
    }
    
    ...
                   new  JniInvokeMethod().nativeCallback(new JniInvokeMethod.CallBack() {
                       @Override
                       public void doBack() {
                           log("JniInvokeMethod nativeCallback " + Thread.currentThread().getName());
                       }
                   });
                   new  JniInvokeMethod().nativeThreadCallback(new JniInvokeMethod.CallBack() {
                       @Override
                       public void doBack() {
                           log("JniInvokeMethod nativeThreadCallback " + Thread.currentThread().getName());
                       }
                   });
    

    原理很简单 找到方法执行与 参考 NDK开发-2-数据交换

    extern "C"
    JNIEXPORT void JNICALL
    **_nativeCallback(JNIEnv *env, jobject thiz,
                                                                       jobject call_back) {
        jclass jclass1 = env->GetObjectClass(call_back);
        jmethodID jmethodId = env->GetMethodID(jclass1, "doBack", "()V");
        env->CallVoidMethod(call_back, jmethodId);
    }
    

    异步回调

    JavaVM *g_VM;
    jobject threadObject;
    
    
    void *threadCallback(void *) {
    // env 不能跨线程 子线程中运行的
        JNIEnv *env;
        if(g_VM->AttachCurrentThread(&env, NULL)==0){
            /*找到方法回调*/
            jclass threadClass = env->GetObjectClass(threadObject);
            jmethodID threadMethod = env->GetMethodID(threadClass, "doBack", "()V");
            env->CallVoidMethod(threadObject, threadMethod);
            /*AttachCurrentThread成对使用*/
            g_VM->DetachCurrentThread();
        }
        return 0;
    }
    
    extern "C"
    JNIEXPORT void JNICALL
    ***nativeThreadCallback(JNIEnv *env, jobject thiz,
                                                                             jobject call_back) {
        env->GetJavaVM(&g_VM); /*保存env对象*/
        threadObject = env->NewGlobalRef(call_back); /*创建全局的对象,否则子线程是找不到的*/
        pthread_t handle;
        pthread_create(&handle, NULL, threadCallback, NULL);
    }
    
    
    `

    相关文章

      网友评论

        本文标题:NDK开发-3回调 返回java实例

        本文链接:https://www.haomeiwen.com/subject/gdnlhhtx.html