http://blog.csdn.net/xzx735/article/details/23679333
Code Example 2-1 Implementing a Native Method Using C
jdouble Java_pkg_Cls_f__ILjava_lang_String_2 (
JNIEnv *env, /* interface pointer */
jobject obj, /* "this" pointer */
jint i, /* argument #1 */
jstring s) /* argument #2 */
{
/* Obtain a C-copy of the Java string */
const char *str = (*env)->GetStringUTFChars(env, s, 0);
/* process the string */
...
/* Now we are done with str */
(*env)->ReleaseStringUTFChars(env, s, str);
return ...
}
Note that we always manipulate Java objects using the interface pointer env . Using C++, you can write a slightly cleaner version of the code, as shown in Code Example 2-2:
Code Example 2-2 Implementing a Native Method Using C++
extern "C" /* specify the C calling convention */
jdouble Java_pkg_Cls_f__ILjava_lang_String_2 (
JNIEnv *env, /* interface pointer */
jobject obj, /* "this" pointer */
jint i, /* argument #1 */
jstring s) /* argument #2 */
{
const char *str = env->GetStringUTFChars(s, 0);
...
env->ReleaseStringUTFChars(s, str);
return ...
{
JNIEXPORT jint JNICALL Java_com_forlinx_android_HardwareInterface_runSystemCmd
(JNIEnv *env, jobject obj,jstring jcmd)
{
int ret;
const char *cmd = NULL;
cmd = jcmd ? env->GetStringUTFChars(jcmd, NULL) : NULL;
if (!cmd) {
jniThrowNullPointerException(env, "cmd");
return -EINVAL;
}
ret = system(cmd);
if(ret < 0) {
ALOGW("Unable to run cmd=%s %s\n",cmd,strerror(errno));
ret = -1;
}
env->ReleaseStringUTFChars(jcmd, cmd);
return ret;
}
}//end namespace android
Code Example 2-1 Implementing a Native Method Using C
jdouble Java_pkg_Cls_f__ILjava_lang_String_2 (
JNIEnv *env, /* interface pointer */
jobject obj, /* "this" pointer */
jint i, /* argument #1 */
jstring s) /* argument #2 */
{
/* Obtain a C-copy of the Java string */
const char *str = (*env)->GetStringUTFChars(env, s, 0);
/* process the string */
...
/* Now we are done with str */
(*env)->ReleaseStringUTFChars(env, s, str);
return ...
}
Note that we always manipulate Java objects using the interface pointer env . Using C++, you can write a slightly cleaner version of the code, as shown in Code Example 2-2:
Code Example 2-2 Implementing a Native Method Using C++
extern "C" /* specify the C calling convention */
jdouble Java_pkg_Cls_f__ILjava_lang_String_2 (
JNIEnv *env, /* interface pointer */
jobject obj, /* "this" pointer */
jint i, /* argument #1 */
jstring s) /* argument #2 */
{
const char *str = env->GetStringUTFChars(s, 0);
...
env->ReleaseStringUTFChars(s, str);
return ...
}
jni意常处理
cpp
Android.mk
LOCAL_SHARED_LIBRARIES := libc libutils libcutils libnativehelper
#include "JNIHelp.h"
#include <jni.h>
#include <utils/Log.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <linux/ioctl.h>
#define LOG_TAG "run-system-cmd"
{
JNIEXPORT jint JNICALL Java_com_forlinx_android_HardwareInterface_runSystemCmd
(JNIEnv *env, jobject obj,jstring jcmd)
{
int ret;
const char *cmd = NULL;
cmd = jcmd ? env->GetStringUTFChars(jcmd, NULL) : NULL;
if (!cmd) {
jniThrowNullPointerException(env, "cmd");
return -EINVAL;
}
ret = system(cmd);
if(ret < 0) {
ALOGW("Unable to run cmd=%s %s\n",cmd,strerror(errno));
ret = -1;
}
env->ReleaseStringUTFChars(jcmd, cmd);
return ret;
}
}//end namespace android