现在的位置: 首页 > 综合 > 正文

JNI 对象数组传递

2018年02月10日 ⁄ 综合 ⁄ 共 6824字 ⁄ 字号 评论关闭

/*

* Copyright (C) 2009 The Android Open Source Project

*

* Licensed under the Apache License, Version 2.0 (the "License");

* you may not use this file except in compliance with the License.

* You may obtain a copy of the License at

*

*      http://www.apache.org/licenses/LICENSE-2.0

*

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and

* limitations under the License.

*/

package com.example.hellojni;

 

import java.util.ArrayList;

 

import android.app.Activity;

import android.widget.TextView;

import android.os.Bundle;

 

public class HelloJni extends Activity {

        /** Called when the activity is first created. */

        @Override

        public void onCreate(Bundle savedInstanceState) {

                super.onCreate(savedInstanceState);

 

                /*

                 * Create a TextView and set its content. the text is retrieved by

                 * calling a native function.

                 */

                TextView tv = new TextView(this);

                tv.setText(stringFromJNI());

                setContentView(tv);

                ArrayList<String> arrayList = new ArrayList<String>();

                arrayList.add("hello");

                arrayList.add("jni");

                arrayList.add("i am java");

                arrayListFromJava(arrayList);

                dataMemberFromJava();

                String[] strArray = { "china", "company" };

                stringArrayFromJava(strArray);

        }

 

        /*

         * A native method that is implemented by the 'hello-jni' native library,

         * which is packaged with this application.

         */

        public native String stringFromJNI();

 

        /*

         * This is another native method declaration that is *not* implemented by

         * 'hello-jni'. This is simply to show that you can declare as many native

         * methods in your Java code as you want, their implementation is searched

         * in the currently loaded native libraries only the first time you call

         * them.

         *

         * Trying to call this function will result in a

         * java.lang.UnsatisfiedLinkError exception !

         */

        public native String unimplementedStringFromJNI();

 

        /*

         * this is used to load the 'hello-jni' library on application startup. The

         * library has already been unpacked into

         * /data/data/com.example.HelloJni/lib/libhello-jni.so at installation time

         * by the package manager.

         */

        static {

                System.loadLibrary("hello-jni");

        }

 

        private static String mClassName = "HelloJni.java";

        private static int mClassID = 10000;

        private String mInstanceName = "This is string for HelloJni.class' instance";

        private int mInstanceId = 8888;

 

        public native void arrayListFromJava(ArrayList<String> array);

 

        public native void dataMemberFromJava();

 

        public native void stringArrayFromJava(String[] strArray);

 

}

[ /code]这个是c++代码[code]/*

* Copyright (C) 2009 The Android Open Source Project

*

* Licensed under the Apache License, Version 2.0 (the "License");

* you may not use this file except in compliance with the License.

* You may obtain a copy of the License at

*

*      http://www.apache.org/licenses/LICENSE-2.0

*

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and

* limitations under the License.

*

*/

#include <string.h>

#include <jni.h>

#include <stdlib.h>

#include <stdio.h>

#include <android/log.h>

 

#ifndef     LOG_TAG

#define     LOG_TAG "hello-jni"

#endif

 

#define     LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)

#define     LOGD(...) __android_log_print(ANDROID_LOG_DEBUG  , LOG_TAG, __VA_ARGS__)

#define     LOGI(...) __android_log_print(ANDROID_LOG_INFO   , LOG_TAG, __VA_ARGS__)

#define     LOGW(...) __android_log_print(ANDROID_LOG_WARN   , LOG_TAG, __VA_ARGS__)

#define     LOGE(...) __android_log_print(ANDROID_LOG_ERROR  , LOG_TAG, __VA_ARGS__)

 

using namespace std;

 

/* This is a trivial JNI example where we use a native method

* to return a new VM String. See the corresponding Java source

* file located at:

*

*   apps/samples/hello-jni/project/src/com/example/HelloJni/HelloJni.java

*/

 

jstring Java_com_example_hellojni_HelloJni_stringFromJNI(JNIEnv* env, jobject thiz)

{

 

        return (env)->NewStringUTF("Hello from JNI !");

}

void arrayListFromJava(JNIEnv* env, jobject thiz, jobject arrayList)

{

        jclass clazz = (env)->GetObjectClass(arrayList);

        jmethodID getMethodID = (env)->GetMethodID(clazz, "get", "(I)Ljava/lang/Object;");

        jmethodID sizeMethodID = (env)->GetMethodID(clazz, "size", "()I");

        int size = (env)->CallIntMethod(arrayList, sizeMethodID);

        LOGE("arrayList's size is : %d", size);

        for (int i = 0; i < size; i++)

        {

                jstring str = (jstring)(env)->CallObjectMethod(arrayList, getMethodID, i);

                jboolean isCpopy;

                const char * p = (env)->GetStringUTFChars(str, &isCpopy);

                LOGE(p);

        }

 

}

void dataMemberFromJava(JNIEnv* env, jobject thiz)

{

 

        LOGE("this shows how we can get class member data  and instance data from java.!!! ");

        jclass clazz = (env)->GetObjectClass(thiz);

        jfieldID id = (env)->GetStaticFieldID(clazz, "mClassID", "I");

        int mClassID = (env)->GetStaticIntField(clazz, id);

        LOGE("mClassID's value is : %d", mClassID);

 

        id = (env)->GetFieldID(clazz, "mInstanceId", "I");

        int mInstanceId = (env)->GetIntField(thiz, id);

        LOGE("mInstanceId's value is : %d", mInstanceId);

 

        id = (env)->GetStaticFieldID(clazz, "mClassName", "Ljava/lang/String;");

        jstring mClassName = (jstring)(env)->GetStaticObjectField(clazz, id);

        LOGE((env)->GetStringUTFChars(mClassName,NULL));

 

}

void stringArrayFromJava(JNIEnv* env, jobject thiz, jobjectArray array)

{

        LOGE("stringArrayFromJava");

        jsize size = (env)->GetArrayLength(array);

        for (int i = 0; i < size; i++)

        {

                jstring str = (jstring)(env)->GetObjectArrayElement(array, i);

                LOGE((env)->GetStringUTFChars(str,NULL));

        }

 

}

static JNINativeMethod const gMethods[] =

{

{ "stringFromJNI", "()Ljava/lang/String;", (void*) Java_com_example_hellojni_HelloJni_stringFromJNI },

{ "arrayListFromJava", "(Ljava/util/ArrayList;)V", (void*) arrayListFromJava },

{ "dataMemberFromJava", "()V", (void*) dataMemberFromJava },

{ "stringArrayFromJava", "([Ljava/lang/String;)V", (void*) stringArrayFromJava }, };

 

jint JNI_OnLoad(JavaVM *jvm, void *reserved)

{

        JNIEnv* env = NULL;

        if (jvm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK)

        {

                LOGD(">>>Get env faild!!!");

                return -1;

        }

        jclass clazz = (env)->FindClass("com/example/hellojni/HelloJni");

        if (!clazz)

        {

                LOGE(">>>>Can't find com/example/hellojni/HelloJn class!!! ");

        }

        (env)->RegisterNatives(clazz, gMethods, sizeof(gMethods) / sizeof(gMethods[0]));

        LOGD(">>>>JNI_OnLoad Called!!!");

        return JNI_VERSION_1_4;

 

}

 

void JNI_OnUnload(JavaVM *jvm, void *reserved)

{

        LOGD(">>>>JNI_OnUnload Called!!!");

        return;

}

抱歉!评论已关闭.