jnihelp.h头文件根据需求自行添加,也可以直接删除

#define LOG_TAG "JNIHelp"
#include "JNIHelp.h"
#include "android/log.h"
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>


static void getExceptionSummary(JNIEnv* env, jthrowable exception, char* buf, size_t bufLen)
{
int success = 0;

/* get the name of the exception's class */
jclass exceptionClazz = env->GetObjectClass(exception); // can't fail
jclass classClazz = env->GetObjectClass(exceptionClazz); // java.lang.Class, can't fail
jmethodID classGetNameMethod = env->GetMethodID(classClazz, "getName", "()Ljava/lang/String;");
jstring classNameStr = static_cast<jstring>(env->CallObjectMethod(exceptionClazz, classGetNameMethod));
if (classNameStr != NULL) {
/* get printable string */
const char* classNameChars = env->GetStringUTFChars( classNameStr, NULL);
if (classNameChars != NULL) {
/* if the exception has a message string, get that */
jmethodID throwableGetMessageMethod = env->GetMethodID(
exceptionClazz, "getMessage", "()Ljava/lang/String;");
jstring messageStr = static_cast<jstring>(env->CallObjectMethod(exception, throwableGetMessageMethod));

if (messageStr != NULL) {
const char* messageChars = env->GetStringUTFChars( messageStr, NULL);
if (messageChars != NULL) {
snprintf(buf, bufLen, "%s: %s", classNameChars, messageChars);
env->ReleaseStringUTFChars( messageStr, messageChars);
} else {
env->ExceptionClear(); // clear OOM
snprintf(buf, bufLen, "%s: <error getting message>", classNameChars);
}
env->DeleteLocalRef( messageStr);
} else {
strncpy(buf, classNameChars, bufLen);
buf[bufLen - 1] = '\0';
}

env->ReleaseStringUTFChars( classNameStr, classNameChars);
success = 1;
}
env->DeleteLocalRef( classNameStr);
}
env->DeleteLocalRef( classClazz);
env->DeleteLocalRef( exceptionClazz);

if (! success) {
env->ExceptionClear();
snprintf(buf, bufLen, "%s", "<error getting class name>");
}
}
/* Throw an exception with the specified class and an optional message.*/
int jniThrowException(JNIEnv* env)
{
jclass exceptionClass;
int result = 0;

if (env->ExceptionCheck()) {
/* TODO: consider creating the new exception with this as "cause" */
char buf[256];

jthrowable exception = env->ExceptionOccurred();
env->ExceptionClear();
if (exception != NULL) {
getExceptionSummary(env,exception, buf, sizeof(buf));
//LOGW("Discarding pending exception (%s) to throw %s\n", buf, className);
__android_log_print(ANDROID_LOG_INFO, "", "Discarding pending exception (%s) to throw\n",buf);
env->DeleteLocalRef(exception);
result = -1;
}
}
return result;
}


更多相关文章

  1. 谁说Android不行了,以个人能力的不行来否定整个市场的需求是弱者
  2. Android 企业需求与开发者状况简析(转)
  3. IKNinePhotoView 是一个开源的 Android 九宫格控件,可以自适应宽
  4. Android商城开发(一)——一次活动页需求引发的危机感
  5. Android6.0 MTK 需求文档(五)
  6. Android实现录屏直播(二)需求才是硬道理之产品功能调研
  7. 如何优雅地运用位运算实现产品需求
  8. 非功能性需求,不要成为项目的坑
  9. 漫画 | 如何向外行解释产品经理频繁更改需求会令程序员很焦灼?

随机推荐

  1. Android(安卓)EditText 限制只能输入指定
  2. Android上对非标准RSS进行解析之后的反思
  3. 《Android(安卓)Studio实用指南》4.27 使
  4. 为什么Android将掀起一场手机领域的狂风(
  5. No suitable java found.In order to pro
  6. 浅谈应用工厂模式和单例在Android中实现
  7. Android(安卓)中的进程
  8. Android-几行代码通知系统自动安装apk
  9. 在android的spinner中,实现key(id)和Value
  10. 【Android学习总结】之eclipse工程目录结