标签:nic lease 通过 增加 from 可变参 n+1 rom text
Java传递数据给C例子:
public class JNI {
static{
System.loadLibrary("passdata");
}
//传递两个int类型的变量给C 让C加一下返回来
public native int add(int x, int y);
//传递String类型的参数给C 处理一下返回来
public native String sayHelloInC(String s);
//传递int类型的数组给C
public native int[] arrElementsIncrease(int[] intArray);
}
C语言中编写:
#include <jni.h>
#include <stdlib.h>
#include <android/log.h>
#define LOG_TAG "System.out"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
JNIEXPORT jint JNICALL Java_com_itheima_javapassdata_JNI_add
(JNIEnv * env, jobject clazz, jint x, jint y){
return x+y;
}
/**
* 把一个jstring转换成一个c语言的char* 类型.
*/
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_itheima_javapassdata_JNI_sayHelloInC
(JNIEnv * env, jobject clazz, jstring jstr){
//调用工具方法把 java中的string 类型 转换成 C 语言中的 char*
char* cstr = _JString2CStr(env,jstr);
//调用strlen 获取 cstr 字符串的长度
int length = strlen(cstr);
int i;
for(i = 0;i<length;i++){
*(cstr+i) += 1;
}
return (*env)->NewStringUTF(env,cstr);
}
JNIEXPORT jintArray JNICALL Java_com_itheima_javapassdata_JNI_arrElementsIncrease
(JNIEnv * env, jobject clazz, jintArray jArray){
//jsize (*GetArrayLength)(JNIEnv*, jarray);
jsize length =(*env)->GetArrayLength(env,jArray);
LOGD("length = %d",length);
//jboolean iscopy;
//jint* (*GetIntArrayElements)(JNIEnv*, jintArray, jboolean*);
int* arrayPointer =(*env)->GetIntArrayElements(env,jArray,NULL);
int i;
for(i = 0;i<length;i++){
*(arrayPointer+i) += 10;
}
return jArray;
}
Android.mk文件增加以下内容
LOCAL_LDLIBS += -llog
C代码中增加以下内容
#include <android/log.h>
#define LOG_TAG "System.out"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
例子:
public class JNI {
static{
System.loadLibrary("callback");
}
private Context mContext;
public JNI(Context context){
mContext = context;
}
public native void callbackvoidmethod();
public native void callbackintmethod();
public native void callbackStringmethod();
public native void callbackShowToast();
//C调用java空方法
public void helloFromJava(){
System.out.println("hello from java");
}
//C调用java中的带两个int参数的方法
public int add(int x,int y) {
return x+y;
}
//C调用java中参数为string的方法
public void printString(String s){
System.out.println(s);
}
public void showToast(String s){
Toast.makeText(mContext, s, 0).show();
}
}
C文件中:
#include <jni.h>
#include <stdlib.h>
#include <android/log.h>
#define LOG_TAG "System.out"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
/**
* 把一个jstring转换成一个c语言的char* 类型.
*/
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 void JNICALL Java_com_itheima_callbackjava_JNI_callbackvoidmethod
(JNIEnv * env, jobject clazz){
//jclass (*FindClass)(JNIEnv*, const char*);
//① 获取字节码对象
jclass claz = (*env)->FindClass(env,"com/itheima/callbackjava/JNI");
//②获取Method对象,第四个参数为方法签名
//jmethodID (*GetMethodID)(JNIEnv*, jclass, const char*, const char*);
jmethodID methodID =(*env)->GetMethodID(env,claz,"helloFromJava","()V");
//③通过字节码对象创建一个Object
//④通过对象调用方法
//void (*CallVoidMethod)(JNIEnv*, jobject, jmethodID, ...);
(*env)->CallVoidMethod(env,clazz,methodID);
}
JNIEXPORT void JNICALL Java_com_itheima_callbackjava_JNI_callbackintmethod
(JNIEnv * env, jobject clazz){
//① 获取字节码对象
jclass claz =(*env)->FindClass(env,"com/itheima/callbackjava/JNI");
//②获取Method对象
jmethodID methodID = (*env)->GetMethodID(env,claz,"add","(II)I");
//jint (*CallIntMethod)(JNIEnv*, jobject, jmethodID, ...);
int result =(*env)->CallIntMethod(env,clazz,methodID,3,4);
LOGD("result = %d",result);
}
JNIEXPORT void JNICALL Java_com_itheima_callbackjava_JNI_callbackStringmethod
(JNIEnv * env, jobject clazz){
//① 获取字节码对象
jclass claz =(*env)->FindClass(env,"com/itheima/callbackjava/JNI");
//② 获取Method对象
jmethodID methodid =(*env)->GetMethodID(env,claz,"printString","(Ljava/lang/String;)V");
//
jstring result =(*env)->NewStringUTF(env,"hello from c");
(*env)->CallVoidMethod(env,clazz,methodid,result);
}
JNIEXPORT void JNICALL Java_com_itheima_callbackjava_JNI_callbackShowToast
(JNIEnv * env, jobject clazz){
jclass claz =(*env)->FindClass(env,"com/itheima/callbackjava/JNI");
jmethodID methodid =(*env)->GetMethodID(env,claz,"showToast","(Ljava/lang/String;)V");
//jobject (*AllocObject)(JNIEnv*, jclass);
//通过字节码对象创建 java对象 在这儿就是创建了mainactivity的对象
//jobject obj =(*env)->AllocObject(env,claz);
jstring result =(*env)->NewStringUTF(env,"hello from c");
//void (*CallVoidMethod)(JNIEnv*, jobject, jmethodID, ...);
(*env)->CallVoidMethod(env,clazz,methodid,result);
}
JNI学习笔记2-Java传给C-C代码中向logcat输出内容-C代码回调java方法
标签:nic lease 通过 增加 from 可变参 n+1 rom text
原文地址:https://www.cnblogs.com/lw-blog-666/p/8979301.html