package com.itheima.charencode; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.EditText; public class MainActivity extends Activity { private EditText et; static{ System.loadLibrary("hello");//导入类库 } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et = (EditText) findViewById(R.id.et); } public void click1(View v){ String text = et.getText().toString(); et.setText(encode(text, text.length())); } public void click2(View v){ String text = et.getText().toString(); et.setText(decode(text, text.length())); } public native String encode(String text, int length); public native String decode(String text, int length); }
#include <jni.h> #include <string.h> //把java的字符串转换成c的字符串 char* Jstring2CStr(JNIEnv* env, jstring jstr) { char* rtn = NULL; jclass clsstring = (*env)->FindClass(env,"java/lang/String"); jstring strencode = (*env)->NewStringUTF(env,"GB2312"); jmethodID mid = (*env)->GetMethodID(env,clsstring, "getBytes", "(Ljava/lang/String;)[B"); jbyteArray barr= (jbyteArray)(*env)->CallObjectMethod(env,jstr,mid,strencode); // String .getByte("GB2312"); jsize alen = (*env)->GetArrayLength(env,barr); jbyte* ba = (*env)->GetByteArrayElements(env,barr,JNI_FALSE); if(alen > 0) { rtn = (char*)malloc(alen+1); //" " memcpy(rtn,ba,alen); rtn[alen]=0; } (*env)->ReleaseByteArrayElements(env,barr,ba,0); // return rtn; } JNIEXPORT jstring JNICALL Java_com_itheima_charencode_MainActivity_encode (JNIEnv * env, jobject obj, jstring jstr, jint lenght){ char* cstr = Jstring2CStr(env, jstr); int i; for(i = 0;i < lenght; i++){ *(cstr + i) += 1; } return (*env)->NewStringUTF(env, cstr); } /* * Class: com_itheima_charencode_MainActivity * Method: decode * Signature: (Ljava/lang/String;)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_com_itheima_charencode_MainActivity_decode (JNIEnv * env, jobject obj, jstring jstr, jint lenght){ char* cstr = Jstring2CStr(env, jstr); int i; for(i = 0;i < lenght; i++){ *(cstr + i) -= 1; } return (*env)->NewStringUTF(env, cstr); }
数组处理:
package com.itheima.array; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; public class MainActivity extends Activity { static{ System.loadLibrary("hello"); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } int[] arr = {1,2,3,4,5}; public void click(View v){ arrayEncode(arr); for (int i : arr) {//上面已经对数组内存进行了改变,所以此处是修改后的数组 System.out.println(i); } } public native void arrayEncode(int[] arr); } /* hello.c文件: #include <jni.h> JNIEXPORT void JNICALL Java_com_itheima_array_MainActivity_arrayEncode (JNIEnv * env, jobject obj, jintArray jintarr){//jintArray是整型数组,jni中所有的数组都是指针类型 //拿到整型数组的长度以及第0个元素的地址 //jsize (*GetArrayLength)(JNIEnv*, jarray);jsize就是int类型(jni.h文件都有定义) int length = (*env)->GetArrayLength(env, jintarr); //jint* (*GetIntArrayElements)(JNIEnv*, jintArray, jboolean*);jint就是int类型 int* arrp = (*env)->GetIntArrayElements(env, jintarr, 0);//得到第0个元素的地址 int i; for(i = 0;i < length; i++){//数组是连续的内存空间,有了数组长度和数组第0个元素地址就相当于拿到了所有数组。 *(arrp + i) += 10;//每个元素加10 } }*/