JNI文件:HelloWorld.cpp
/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class com_lawrence_hello_HelloWorld */ //#include "HelloWorld.h" #include "stdio.h" #include "SerialTool.h" #ifdef __cplusplus extern "C" { #endif struct HelloClass { jfieldID num; jfieldID tax; }; struct CompanyClass { jfieldID title; jfieldID id; jfieldID days; jfieldID money; }; /* * Class: com_lawrence_hello_HelloWorld * Method: print * Signature: ()V */ JNIEXPORT jint JNICALL Java_com_lawrence_hello_HelloWorld_print (JNIEnv *_env, jobject thiz) { struct HelloClass hello_class; struct CompanyClass company_class; jstring StrNum = charToString(_env, "no"); LOGI("Java_com_lawrence_hello_HelloWorld_print start\n"); jclass class1 = _env->FindClass("com/lawrence/hello/HelloWorld"); hello_class.num = _env->GetFieldID(class1, "mNum", "I"); LOGI("1=GetFieldID\n"); hello_class.tax = _env->GetFieldID(class1, "mTax", "F"); LOGI("2=GetFieldID\n"); _env->SetIntField(thiz, hello_class.num, 12); _env->SetFloatField(thiz, hello_class.tax, 1.0); LOGI("SetIntField\n"); LOGI("Java_com_lawrence_hello_HelloWorld_print end\n"); return 9; } /* * Class: Java_com_lawrence_hello_HelloWorld_getInfo * Method: getInfo * Signature: */ JNIEXPORT jobject JNICALL Java_com_lawrence_hello_HelloWorld_getInfo (JNIEnv *_env, jobject thiz) { struct CompanyClass company_class; jstring StrNum = charToString(_env, "no"); //内部类用$ jclass class2 = _env->FindClass("com/lawrence/hello/HelloWorld$company"); LOGI("3=GetFieldID\n"); company_class.title = _env->GetFieldID(class2, "mTitle", "Ljava/lang/String;"); LOGI("4=GetFieldID\n"); company_class.id = _env->GetFieldID(class2, "mId", "B"); LOGI("4=GetFieldID\n"); company_class.days = _env->GetFieldID(class2, "mDays", "I"); company_class.money = _env->GetFieldID(class2, "mMoney", "[I"); jobject joCompany = _env->AllocObject(class2); LOGI("5=GetFieldID\n"); _env->SetObjectField(joCompany, company_class.title, StrNum); _env->SetByteField(joCompany, company_class.id, 20); _env->SetIntField(joCompany, company_class.days, 6); int abc[3] = {55, 10, 4}; //创建一个新的java数组(jarray),但是jarray不是C数组类型,不能直接访问jarray jintArray jiArr = _env->NewIntArray(24); jint *ji; #if 1 //可用 LOGI("6=GetFieldID\n"); //JNI支持一系列的Get/Release<Type>ArrayElement 函数,允许本地代码获取一个指向基本C类型数组的元素的指针。 ji = _env->GetIntArrayElements(jiArr, 0); memcpy(ji, abc, 2 * sizeof(int)); _env->ReleaseIntArrayElements(jiArr, ji, 0); //可加,可不加 _env->SetObjectField(joCompany, company_class.money, jiArr); #else //可用 //可以使用GetIntArrayRegion函数来把一个 int数组中的所有元素复制到一个C缓冲区中 //SetIntArrayRegion则是逆过程 LOGI("7=GetFieldID\n"); _env->SetIntArrayRegion(jiArr, 0, 2, abc); _env->SetObjectField(joCompany, company_class.money, jiArr); #endif LOGI("Java_com_lawrence_hello_HelloWorld_print end\n"); return joCompany; } #ifdef __cplusplus } #endif
java文件:HelloWorld.java
package com.lawrence.hello; import android.support.v7.appcompat.R.integer; public class HelloWorld { public native int print(); public native company getInfo(); public int mNum; public float mTax; public company com; public static class company { public String mTitle; //String类型 public byte mId; //Byte类型 private int mDays; //private: int类型 private int[] mMoney; //private: int基本数组类型 public int getDays() { return mDays; } public String getMoneyStr() { if(mMoney.length > 0) { return Integer.toString(mMoney[0]) + " " + Integer.toString(mMoney[1]); } else { return "NULL"; } } public int getMoneyLenth() { return mMoney.length; } } static { System.loadLibrary("HelloWorld"); } }
java主调文件PlaceholderFragment.java
package com.lawrence.ui; import android.content.Context; import android.content.Intent; import android.content.res.ColorStateList; import android.graphics.Color; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v7.appcompat.R.integer; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import com.lawrence.hello.HelloWorld; import com.lawrence.hello.HelloWorld.company; import com.lawrence.sms.R; /** * A placeholder fragment containing a simple view. */ public class PlaceholderFragment extends Fragment { public Context mContext; public Button mOkButton; public HelloWorld mHello; public TextView mTv; public PlaceholderFragment() { mHello = new HelloWorld(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); mContext = getActivity(); mOkButton = (Button)rootView.findViewById(R.id.ok); mTv = (TextView)rootView.findViewById(R.id.getText); mOkButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { switch (view.getId()) { case R.id.ok: // Toast.makeText(getActivity(), "fuck", Toast.LENGTH_SHORT).show(); // Intent intent = new Intent("android.intent.action.htf"); // intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // mContext.startActivity(intent); // company c = native_getCompany(); //new company(); mHello.print(); company cy = mHello.getInfo(); String str = "hello: " + mHello.mNum + "\t" + mHello.mTax; str = str + "\t mId: " + cy.mId + "\t mTitle: " + cy.mTitle + "\t mDays: " + cy.getDays() + "\t mMoneyLenth: " + cy.getMoneyLenth() + "\t mMoney: " + cy.getMoneyStr() + " !"; //Toast.makeText(getActivity(), str, Toast.LENGTH_SHORT).show(); mTv.setText(str); break; default: break; } } }); return rootView; } }