调用第三方库crash异常捕获android

android开启崩溃捕获

void InitCrashReport() {
 LOG(LS_INFO) << "ThrowJNI----异常捕获。。。";
 struct sigaction handler;
 memset(&handler, 0, sizeof(struct sigaction));
 handler.sa_sigaction = android_sigaction;
 handler.sa_flags = SA_RESETHAND;
#define CATCHSIG(X) sigaction(X, &handler, &old_sa[X])
 CATCHSIG(SIGTRAP);
 CATCHSIG(SIGKILL);
 CATCHSIG(SIGKILL);
 CATCHSIG(SIGILL);
 CATCHSIG(SIGABRT);
 CATCHSIG(SIGBUS);
 CATCHSIG(SIGFPE);
 CATCHSIG(SIGSEGV);
 CATCHSIG(SIGSTKFLT);
 CATCHSIG(SIGPIPE);
}

通过singal的设置,当崩溃发生的时候就会调用android_sigaction函数。这同样是linux的信号机制。

获取崩溃信号后,将崩溃信息反馈给上层·:

void android_sigaction(int signal, siginfo_t *info, void *reserved) {
 LOG(LS_INFO) << "ThrowJNI----异常....android_sigaction........";
 JNIEnv *env;
 gs_jvm->AttachCurrentThread(&env, NULL);
 if (!env) {
  return;
 }
 if (qttCallback_class != NULL) {
  jmethodID qttCallback_method = env->GetMethodID(qttCallback_class,
    "", "()V");
  jobject obj_qtt = env->NewObject(qttCallback_class, qttCallback_method,
    "");
  jmethodID gJinMethod = env->GetMethodID(qttCallback_class,
    "getJNIExcaption", "()V");
  env->CallVoidMethod(obj_qtt, gJinMethod);
  env->DeleteLocalRef(obj_qtt);
 }
 old_sa[signal].sa_handler(signal);
}

 

你可能感兴趣的:(jni操作)