C++回调java方法

因为网上大部分是java调用c或者c++的文章较多,所以写一个留个念想。

还是直接上代码再来解释

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
static JavaVM* g_JavaVM = NULL;
static jobject g_InterfaceObject = 0;
static const char *g_JavaClassName = "xxx/xxx/xxxx/Provider";

static void GetInterfaceObject(JNIEnv *env, const char *path, jobject *objptr)
{
jclass cls = env->FindClass(path);
if (!cls)
{
return;
}
jmethodID constr = env->GetMethodID(cls, "<init>", "()V");
if (!constr)
{
return;
}
jobject obj = env->NewObject(cls, constr);
if (!obj)
{
return;
}
(*objptr) = env->NewGlobalRef(obj);
}

jint JNI_OnLoad(JavaVM *vm, void *reserved)
{
g_JavaVM = vm;

JNIEnv *env;
if (vm->GetEnv((void**) &env, JNI_VERSION_1_6) != JNI_OK)
{
return -1;
}

GetInterfaceObject(env, g_JavaClassName, &g_InterfaceObject);

return JNI_VERSION_1_6;

}

static jobject getInstance(JNIEnv *env, jclass obj_class)
{
jmethodID c_id = env->GetMethodID(obj_class, "<init>", "()V");
jobject obj = env->NewObject(obj_class, c_id);
return obj;
}

void ConnectSuccessNotice(const int nWidth, const int nHeight, const int nFps)
{
if (g_JavaVM == NULL)
{
return;
}
int status;
JNIEnv *env = NULL;
bool isAttached = false;

status = g_JavaVM->GetEnv((void**) &env, JNI_VERSION_1_6);

if (status < 0) //
{
status = g_JavaVM->AttachCurrentThread(&env, NULL);
if (status < 0)
{
return;
}
isAttached = true;
}

if (isAttached) //
{
jclass cls = env->GetObjectClass(g_InterfaceObject);
if (cls != 0)
{
jmethodID mid = env->GetStaticMethodID(cls, "ConnectSuccessNoticeJavaThread", "(III)V");
if (mid != 0)
{
env->CallStaticVoidMethod(cls, mid, nWidth, nHeight, nFps);
}
}
g_JavaVM->DetachCurrentThread();
}
else //
{
jclass cls = env->FindClass(g_JavaClassName);
if (cls != 0)
{

jobject obj = getInstance(env, cls);
jmethodID mid = env->GetMethodID(cls, "ConnectSuccessNoticeJava", "(III)V");
if (mid != 0)
{
env->CallVoidMethod(obj, mid, nWidth, nHeight, nFps);
}
}
}

}


void OnDisconnectedNotice()
{
if (g_JavaVM == NULL)
{
return;
}
int status;
JNIEnv *env = NULL;
bool isAttached = false;

status = g_JavaVM->GetEnv((void**) &env, JNI_VERSION_1_6);


if (status < 0)
{
status = g_JavaVM->AttachCurrentThread(&env, NULL);
if (status < 0)
{
return;
}
isAttached = true;
}

if (isAttached) //
{
jclass cls = env->GetObjectClass(g_InterfaceObject);
if (cls != 0)
{
jmethodID mid = env->GetStaticMethodID(cls, "OnDisconnectedJavaThread", "()V");
if (mid != 0)
{
env->CallStaticVoidMethod(cls, mid);
}
}
g_JavaVM->DetachCurrentThread();
}
else //
{
jclass cls = env->FindClass(g_JavaClassName);
if (cls != 0)
{

jobject obj = getInstance(env, cls);
jmethodID mid = env->GetMethodID(cls, "OnDisconnectedJava", "()V");
if (mid != 0)
{
env->CallVoidMethod(obj, mid);
}
}
}

}

void ReceiveDataCallback(const char *nbuff,const int nLength,const int nKey, const int nTimeStamp, string sDeviceID)
{

if (g_JavaVM == NULL)
{
return;
}
int status;
JNIEnv *env = NULL;
bool isAttached = false;

status = g_JavaVM->GetEnv((void**) &env, JNI_VERSION_1_6);

if (status < 0) //
{
status = g_JavaVM->AttachCurrentThread(&env, NULL);
if (status < 0)
{
return;
}
isAttached = true;
}

if (isAttached)
{

jclass cls = env->GetObjectClass(g_InterfaceObject);
if (cls != 0)
{
jmethodID mid = env->GetStaticMethodID(cls, "ReceiveDataCallbackJavaThread", "(Ljava/lang/String;III[B)V");
if (mid != 0)
{
jbyteArray jBufferArray = env->NewByteArray(nLength);
jbyte *jby =env->GetByteArrayElements(jBufferArray, 0);

memcpy(jby, nbuff, nLength);

env->SetByteArrayRegion(jBufferArray, 0, nLength, jby);

jstring jsDeviceID = env->NewStringUTF(sDeviceID.c_str());

env->CallStaticVoidMethod(cls, mid,jsDeviceID, nLength, nKey, nTimeStamp,jBufferArray);

env->ReleaseByteArrayElements(jBufferArray,jby,0);

env->DeleteLocalRef(jBufferArray);
}
}
g_JavaVM->DetachCurrentThread();
}
else // PrintNdkLog不是在线程中被调用, 调用Java类成员API
{

jclass cls = env->FindClass(g_JavaClassName);
if (cls != 0)
{

jobject obj = getInstance(env, cls);
jmethodID mid = env->GetMethodID(cls, "ReceiveDataCallbackJava", "(Ljava/lang/String;III[B)V");
if (mid != 0)
{
jbyteArray jBufferArray = env->NewByteArray(nLength);

jbyte *jby =env->GetByteArrayElements(jBufferArray, 0);

memcpy(jby, nbuff, nLength);

env->SetByteArrayRegion(jBufferArray, 0, nLength, jby);

jstring jsDeviceID = env->NewStringUTF(sDeviceID.c_str());

env->CallVoidMethod(obj, mid,jsDeviceID, nLength, nKey, nTimeStamp,jBufferArray);

env->ReleaseByteArrayElements(jBufferArray,jby,0);

env->DeleteLocalRef(jBufferArray);
}
}
}
}

唯一需要解释的只有两个地方

第一个就是在NDK中创建的线程中, 只允许调用静态的Java API,只能使CallStaticVoidMethod

第二个就是JNI_OnLoad,在这里把JavaVM指针保存下来

重新改方法时,java会先调用该组件的JNI_OnLoad()函数.其用途有二:

一是:告诉java VM此C组件使用那一个JNI版本。如果你没有提供JNI_OnLoad()函数,VM会默认使用最老的JNI 1.1版本。由于新版的JNI做了许多扩充,如果需要使用JNI的新版功能,例如JNI 1.4的java.nio.ByteBuffer,就必须藉由JNI_OnLoad()函数来告知VM。

二是:由于VM执行到System.loadLibrary()函数时,就会立即先呼叫JNI_OnLoad(),所以C组件的开发者可以藉由JNI_OnLoad()来进行C组件内的初期值之设定,也就是获取JavaVM接口。

唯一需注意的就是一定小心内存泄露的问题,java代码如果写多了,也许会不太适应