1、创建布局:
<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="wrap_content" android:orientation="vertical" > <EditText android:id="@+id/et_pwd" android:layout_width="match_parent" android:layout_height="50dp" android:inputType="textPassword" /> <LinearLayout android:layout_width="match_parent" android:gravity="center_horizontal" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/btn_encode" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="encode" android:text="加密" /> <Button android:id="@+id/btn_decode" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="decode" android:text="解密" /> </LinearLayout></LinearLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
2、创建页面
/** * @描述 TODO * @项目名称 App_imooc * @包名 com.android.imooc.jni * @类名 JniActivity * @author chenlin * @date 2013年5月24日 上午6:58:39 * @version 1.0 */public class JniActivity extends Activity { private EditText mEtPwd; static{ System.loadLibrary("hello"); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_jni); mEtPwd = (EditText) findViewById(R.id.et_pwd); Log.i("tag", helloFromC()); } public void encode(View view){ String text = mEtPwd.getText().toString(); if (!TextUtils.isEmpty(text)) { String pwd = encodePwd(text, text.length()); System.out.println("加密密码=" + pwd); mEtPwd.setText(pwd); } } public void decode(View view){ String text = mEtPwd.getText().toString(); if (!TextUtils.isEmpty(text)) { System.out.println("解密密码=" + decodePwd(text, text.length())); } } public native String helloFromC(); public native int sum(int x, int y); public native String encodePwd(String pass, int len); public native String decodePwd(String pass, int len);}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
3、进入项目的src目录下
按住shift+右键,打开cmd窗口
在窗口里输入
javah com.android.imooc.jni.JniActivity
此时会生成文件com_android_imooc_jni_JniActivity
打开
拷贝里面的两个方法到hello.c文件里
JNIEXPORT jstring JNICALL Java_com_android_imooc_jni_JniActivity_encodePwd (JNIEnv *, jobject, jstring, jint)JNIEXPORT jstring JNICALL Java_com_android_imooc_jni_JniActivity_decodePwd (JNIEnv *, jobject, jstring, jint)
4、改写hello.c里的这两个方法
//把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); //"\0" memcpy(rtn, ba, alen); rtn[alen] = 0; } (*env)->ReleaseByteArrayElements(env, barr, ba, 0); // return rtn;}//加密JNIEXPORT jstring JNICALL Java_com_android_imooc_jni_JniActivity_encodePwd( JNIEnv * env, jobject obj, jstring jstr, jint len) { char * cstr = Jstring2CStr(env, jstr); for (int i = 0; i < len; i++) { *(cstr + i) += 1; } return (*env)->NewStringUTF(env, cstr);}//解密JNIEXPORT jstring JNICALL Java_com_android_imooc_jni_JniActivity_decodePwd( JNIEnv * env, jobject obj, jstring jstr, jint len) { char * cstr = Jstring2CStr(env, jstr); for (int i = 0; i < len; i++) { *(cstr + i) -= 1; } return (*env)->NewStringUTF(env, cstr);}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
5、运行,如图
6、输入字母”abc”
得到结果
06-06 14:09:55.561: I/System.out(3956): 加密密码=cde
06-06 14:09:58.437: I/System.out(3956): 解密密码=bcd
0 0