JNI学习笔记(java将数组传递给C)

一.MainActivity代码

package com.study.changearray;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity 
{
    static
    {
        System.loadLibrary("changearray-jni");
    }

    int[] jarr = {5,1,2,3,4,6,9,8,7};

    public native void changeArray(int[] jarr);

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        changeArray(jarr);

        for(int i = 0;i < jarr.length;i++)  
        {
            System.out.println(jarr[i]);
        }
    }

layout代码



    


Android.mk代码

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := changearray-jni
LOCAL_SRC_FILES := changearray-jni.c

include $(BUILD_SHARED_LIBRARY)

changearray-jni.c代码

#include 
#include 


JNIEXPORT void JNICALL Java_com_study_changearray_MainActivity_changeArray
  (JNIEnv *env, jobject obj, jintArray jarr){

    jsize len= (*env)->GetArrayLength(env, jarr);
    jint * cint = (*env)->GetIntArrayElements(env, jarr, NULL);
    int i = 0;
    int j = 0;
    int temp = 0;


    for(i = 0;i < len - 1;i++){
        for(j = 0;j < len - i - 1;j++){
            if(cint[j] > cint[j + 1]){
                temp = cint[j];
                cint[j] = cint[j + 1];
                cint[j + 1] = temp;
            }

        }

    }

}

com_study_changearray_MainActivity.h

#include 

#ifndef _Included_com_study_changearray_MainActivity
#define _Included_com_study_changearray_MainActivity
#ifdef __cplusplus
extern "C" {
#endif

JNIEXPORT void JNICALL Java_com_study_changearray_MainActivity_changeArray
  (JNIEnv *, jobject, jintArray);

#ifdef __cplusplus
}
#endif
#endif

你可能感兴趣的:(安卓,jni,java,c,安卓)