Android-NDK开发之第三个例子--传递整型数组

和以前不同,这次只放代码,不再写步骤,但是,养成一个好的书写步骤是很有必要的,这样你就不容易出错,也容易通过你的步骤来找出错误。特别是你在C/C++代码中出错的时候。

     Android.mk:

    LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := native LOCAL_SRC_FILES := myNative.c include $(BUILD_SHARED_LIBRARY) 

  myNative.c

  #include <stdio.h> #include <stdlib.h> #include <string.h> #include<jni.h> jintArray Java_com_geolo_android_AndoidNDKSample_getIntArray(JNIEnv* env , jobject obj){ int i = 1; jintArray array;//定义数组对象 array = (*env)-> NewIntArray(env, 10); for(; i<= 10; i++){ (*env)->SetIntArrayRegion(env, array, i-1, 1, &i); } return array; } 

AndoidNDKSample.java

package com.geolo.android; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class AndoidNDKSample extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView textView = (TextView)findViewById(R.id.text); int myArray[] = getIntArray(); String myArrayStr = ""; for(int my : myArray){ myArrayStr += ("my: "+ my + "/n"); } textView.setText("getIntArray: " + myArrayStr ); } static{ System.loadLibrary("native"); } public native int[] getIntArray(); } 

main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout>  

你可能感兴趣的:(android,Module,layout,include,library,encoding)