我正在开发 Android 应用程序,我在其中使用 NDK 来做一些小事。如何将字符串从 Java 代码发送到 native C。我想在从 Java 代码发送的 native C 代码中获取一个值。
这是我在 Activity 中编写的代码
observer("test@gmail.com");
public native void observer(String email);
Native代码是这样的
void
Java_pl_pelotasplus_actionafteruninstall_MainActivity_observer(JNIEnv* env, jobject thiz) {
// I want to get email.. How to get
}
提前致谢。
最佳答案
始终使用 javah生成 header ,这样您就不会出现任何错误。还建议将其粘贴在批处理文件中,以便您轻松更新。
来自源文件夹:
...\src> javah pl.pelotasplus.actionafteruninstall.MainActivity
将生成以下.h 文件:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for pl_pelotasplus_actionafteruninstall_MainActivity */
#ifndef _Included_pl_pelotasplus_actionafteruninstall_MainActivity
#define _Included_pl_pelotasplus_actionafteruninstall_MainActivity
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: pl_pelotasplus_actionafteruninstall_MainActivity
* Method: observer
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_pl_pelotasplus_actionafteruninstall_MainActivity_observer
(JNIEnv *, jobject, jstring);
#ifdef __cplusplus
}
#endif
#endif
请注意 header 不包含参数名称,因此在实现时添加它们:
JNIEXPORT void JNICALL Java_pl_pelotasplus_actionafteruninstall_MainActivity_observer
(JNIEnv *env, jobject thiz, jstring email){
const char *nativeEmailString = (*env)->GetStringUTFChars(env, email, 0);
// use your string
(*env)->ReleaseStringUTFChars(env, email, nativeEmailString);
}
关于java - 如何在Android中将字符串从java代码发送到 native C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31381136/