JNI中的java接口使用
项目需求,需要在c++函数中监听相应的状态,并在java端进行一些列的处理。
这个需要在JNI中写一个subscriber,注册后在需要的地方进行引入使用。
目录结构
初始化是AS上的c++工程文件,这边先暂时实现简单的demo,CdemoActivity是NativeActivity的实现,我们暂时别管,因为实现是c++层控制的,有兴趣可以去百度下
主要涉及jnicallback等c文件和JNIUtil这java文件
JNIUtil
public class JNIUtil {
static {
System.loadLibrary("native-event");
}
// 注册函数
public static native void setJniCallBack(JniCallBack callBack);
// 解注册函数
public static native void unJniCallBack();
public interface JniCallBack{
void onReceiveCInfo();
}
}
jnicallback.h
//
// Created by 86130 on 2020/9/9.
//
#ifndef CDEMO_JNICALLBACK_H
#define CDEMO_JNICALLBACK_H
#ifdef __cplusplus
extern "C"{
#endif
//方法回调
void onReceiveMsg();
#ifdef __cplusplus
}
#endif
#endif //CDEMO_JNICALLBACK_H
jnicallback.cpp
#include#include #include #include "jnicallback.h" #ifdef __cplusplus extern "C"{ #endif JavaVM *g_VM; jobject subscriber; extern "C" JNIEXPORT void JNICALL Java_com_demo_cdemo_JNIUtil_setJniCallBack(JNIEnv* env,jclass clazz, jobject call_back) { env->GetJavaVM(&g_VM); subscriber = env->NewGlobalRef(call_back); } extern "C" JNIEXPORT void JNICALL Java_com_demo_cdemo_JNIUtil_unJniCallBack(JNIEnv* env, jclass clazz) { if (subscriber != NULL) { env->DeleteGlobalRef(subscriber); } } JNIEnv *getEnv() { JNIEnv *env; if (g_VM ==NULL) { return NULL; } int envStat = g_VM->GetEnv((void **) &env, JNI_VERSION_1_6); if (envStat == JNI_EDETACHED) { if (g_VM->AttachCurrentThread(&env, NULL) != 0) { return NULL; } } return env; } jmethodID getMethodIdByNameAndSig(JNIEnv *env, const char *name, const char *sig) { if (env == NULL || subscriber == NULL) { return NULL; } jclass subscriberClass = env->GetObjectClass(subscriber); if (subscriber == 0) { return NULL; } jmethodID methodId = env->GetMethodID(subscriberClass, name, sig); if (methodId == 0) { return NULL; } return methodId; } // 头文件方法实现 void onReceiveMsg() { JNIEnv *env = getEnv(); jmethodID onReceiveMethodId = getMethodIdByNameAndSig(env, "onReceiveCInfo", "()V"); if (onReceiveMethodId == NULL) { return; } env->CallVoidMethod(subscriber, onReceiveMethodId); } #ifdef __cplusplus } #endif
在其他的cpp文件中引入jnicallback的头文件就可以使用相应的方法。
CMake文件
project(Native-Activity)
cmake_minimum_required(VERSION 3.4.1)
#引入native_app_glue头文件
include_directories(F:/AndroidSdk/Sdk/ndk-bundle/sources/android/native_app_glue)
add_library(native-activity SHARED
main.cpp
jnicallback.cpp)
add_library(native-event SHARED jnicallback.cpp)
find_library(log-lib log)
find_library(OPENGLES3_LIBRARY GLESv3 "OpenGL ES v3.0 library")
find_library(EGL_LIBRARY EGL "EGL 1.4 library")
find_library(android-lib android)
#编译为静态库
add_library(app_glue STATIC
android_native_app_glue.c)
target_link_libraries(native-event
${log-lib} #链接log库
${android-lib} #链接android库
)
target_link_libraries(native-activity
app_glue #链接静态库native_app_glue
${log-lib} #链接log库
${android-lib} #链接android库
${OPENGLES3_LIBRARY} #链接OpenGLES库
${EGL_LIBRARY} #链接EGL库
)
JNIUtil的使用
package com.demo.cdemo;
import android.app.NativeActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
public class CdemoActivity extends NativeActivity {
static {
System.loadLibrary("native-activity");
}
boolean isFirst = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
JNIUtil.setJniCallBack(new JNIUtil.JniCallBack() {
@Override
public void onReceiveCInfo() {
boolean isMain = isMainThread();
Log.d("zzkk",
"CdemoActivity onReceiveCInfo isMain = " + isMain);
if (!isFirst)
{
isFirst = true;
runonUiThread(new Runnable() {
@Override public void run() {
Intent intent = new Intent(CdemoActivity.this
, MainActivity.class);
startActivity(intent);
}
});
}
}
});
}
private boolean isMainThread() {
return Looper.getMainLooper() == Looper.myLooper();
}
}
可以看见onReceiveCInfo这行日志的打印
综上
到此这篇关于jni中的java接口使用的文章就介绍到这了,更多相关java接口使用内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!



