JNI中的java接口使用
項目需求,需要在c++函數中監聽相應的狀態,并在java端進行一些列的處理。
這個需要在JNI中寫一個subscriber,注冊后在需要的地方進行引入使用。
目錄結構
初始化是AS上的c++工程文件,這邊先暫時實現簡單的demo,CdemoActivity是NativeActivity的實現,我們暫時別管,因為實現是c++層控制的,有興趣可以去百度下
主要涉及jnicallback等c文件和JNIUtil這java文件
JNIUtil
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
// // 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
#include <jni.h> #include <malloc.h> #include <cstring> #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文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
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的使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
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接口使用內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/ZKhero/article/details/108488802