如何将字符串从Java代码发送到Android中的本机C [英] How to get sent string from java code to native C in Android

查看:108
本文介绍了如何将字符串从Java代码发送到Android中的本机C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用android应用程序,其中我正在使用NDK做一些小事情.如何将字符串从Java代码发送到本机C.我想在从Java代码发送的本机C代码中获取一个值.

I am working on android app in which I am using NDK for a small stuff. How to get sent string from java code to native C. I want to get a value in native C code which was sent from java code.

这是我在活动中编写的代码

Here is my code which I wrote in activity

observer("test@gmail.com");
public native void observer(String email);

这是本机代码

void
Java_pl_pelotasplus_actionafteruninstall_MainActivity_observer(JNIEnv* env, jobject thiz) {
    // I want to get email.. How to get
}

谢谢.

推荐答案

始终使用 javah 生成标头,因此您没有任何错误.还建议将其粘贴在批处理文件中,以便轻松进行更新.

Always use javah to generate the headers so you don't have any mistakes. Also recommend sticking it in a batch file so you can update with ease.

从源文件夹:

...\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

请注意,标头不包含参数名称,因此在实现时应添加它们:

Note the header does not include names for the parameters, so add them when implementing like this:

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中的本机C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆