MainActivity
package com.study.strtool;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
static {
System.loadLibrary("strtool-jni");
}
EditText et_str;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_str = (EditText) findViewById(R.id.et_str);
}
public native String strEncode(String jstr);
public native String strDecode(String jstr);
public void encode(View view){
String jstr = et_str.getText().toString().trim();
String en_jstr = strEncode(jstr);
et_str.setText(en_jstr);
}
public void decode(View view){
String jstr = et_str.getText().toString().trim();
String de_jstr = strDecode(jstr);
et_str.setText(de_jstr);
}
}
layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<EditText
android:id="@+id/et_str"
android:hint="输入加密或解密字符串"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="encode"
android:text="加密字符串" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="decode"
android:text="解密字符串" />
</LinearLayout>
com_study_strtool_MainActivity.h
#include <jni.h>
#ifndef _Included_com_study_strtool_MainActivity
#define _Included_com_study_strtool_MainActivity
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT jstring JNICALL Java_com_study_strtool_MainActivity_strEncode
(JNIEnv *, jobject, jstring);
JNIEXPORT jstring JNICALL Java_com_study_strtool_MainActivity_strDecode
(JNIEnv *, jobject, jstring);
#ifdef __cplusplus
}
#endif
#endif
#include <jni.h>
#include <stdio.h>//NULL
#include <stdlib.h>//malloc()/free()
char *strrev(char *s){
char *p = s;
char *q = s;
char temp;
while(*q != '\0')
q++;
while(p < q){
temp = *p;
*p = *q;
*q = temp;
p++;
q--;
}
return s;
}
JNIEXPORT jstring JNICALL Java_com_itheima_strtool_MainActivity_strEncode(
JNIEnv *env, jobject obj, jstring jstr) {
char *cstr = NULL;
jsize len = 0;
jstring new_jstr;
len = (*env)->GetStringLength(env, jstr);
cstr = (char *) malloc(len + 1);//'\0'
(*env)->GetStringUTFRegion(env, jstr, 0, len, cstr);
//逆置字符串 abcd'\0' -- dcba'\0'
cstr = strrev(cstr);
new_jstr = (*env)->NewStringUTF(env, cstr);
free(cstr);
return new_jstr;
}
JNIEXPORT jstring JNICALL Java_com_itheima_strtool_MainActivity_strDecode(
JNIEnv *env, jobject obj, jstring jstr) {
char *cstr = NULL;
jsize len = 0;
jstring new_jstr;
len = (*env)->GetStringLength(env, jstr);
cstr = (char *) malloc(len + 1);//'\0'
(*env)->GetStringUTFRegion(env, jstr, 0, len, cstr);
cstr = strrev(cstr);
new_jstr = (*env)->NewStringUTF(env, cstr);
free(cstr);
return new_jstr;
}
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := strtool-jni
LOCAL_SRC_FILES := strtool-jni.c
include $(BUILD_SHARED_LIBRARY)