Android JNI - Call function on Android UI thread from C++(Android JNI - 从 C++ 调用 Android UI 线程上的函数)
问题描述
我们的游戏引擎 Cocos2d-x
在自己的 non-Java-UI-thread
上以原生方式在 android 上运行.我们需要在 Android UI 线程
上通过 JNI
从 C++
调用某些 Java 函数.
Our game engine Cocos2d-x
runs natively on android on its own non-Java-UI-thread
. We need to call certain Java functions from C++
via JNI
on the Android UI thread
.
为了调用 JNI-Functions
,我们使用的是来自此处 (GitHub) 的 JNIHelper.h/cpp:JniHelper.h, JniHelper.cpp
For calling JNI-Functions
, we're using the JNIHelper.h/cpp from here (GitHub):
JniHelper.h, JniHelper.cpp
例如这个 C++ 代码:
For example this C++ code:
auto retVal = JniHelper::callStaticStringMethod("org/utils/Facebook",
"getFacebookTokenString");
理想情况下,我们希望所有这些调用都发生在 Android UI 线程
上,并将 std::function
作为参数传递,该参数被调用并返回函数调用完成后,再次在 Cocos2d-x-thread
上赋值.
Ideally, we'd like to have all these calls happen on the Android UI thread
and pass an std::function
as a parameter which is called with the return value on the Cocos2d-x-thread
again once the function call is done.
调用函数的理想方式:
auto retVal = JniHelper::callStaticStringMethod("org/utils/Facebook",
"getFacebookTokenString", [=](std::string retVal) {
printf("This is the retval on the C++ caller thread again: %s", retVal.c_str());
});
但是也有很多调用没有任何返回值,所以对于那些调用它们应该更容易在 java 线程上调用它们.
But there are also many calls without any return value, so for those it should be easier to just call them on the java thread.
推荐答案
正如@Elviss 所提到的 - 要将代码发布到主线程,您应该使用 Looper
.实际上,这可以在没有额外处理 JNI 和创建自定义 java.lang.Runnable
并通过复杂的 JNI 东西发布的情况下完成.
As @Elviss has mentioned - to post your code to main thread you should use Looper
. Actually this may be done without extra coping with JNI and creating of custom java.lang.Runnable
and posting it via complicated JNI stuff.
Android NDK 提供了极其轻量和高效的方式来将您的本机代码发布到任意 Looper.关键点是您应该向 looper 提供任意文件描述符并指定您感兴趣的文件事件(输入、输出等).在引擎盖下,looper 将轮询该文件描述符,一旦事件可用 - 它会在正确的线程上运行您的回调.
Android NDK offers extremely lightweight and efficient way to post your native code to the arbitrary looper. The key point is that you should provide arbitrary file descriptor to the looper and specify what file events you are interested in (input, output, so on). Under the hood looper will poll that file descriptor and once event becomes available - it runs your callback on proper thread.
有一个最小的例子(没有错误检查和拆卸):
There is the minimal example (no error checks and teardowns):
#include <android/looper.h>
#include <unistd.h>
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, "sergik", __VA_ARGS__)
static ALooper* mainThreadLooper;
static int messagePipe[2];
static int looperCallback(int fd, int events, void* data);
void someJniFuncThatYouShouldCallOnceOnMainThread() {
mainThreadLooper = ALooper_forThread(); // get looper for this thread
ALooper_acquire(mainThreadLooper); // add reference to keep object alive
pipe(messagePipe); //create send-receive pipe
// listen for pipe read end, if there is something to read
// - notify via provided callback on main thread
ALooper_addFd(mainThreadLooper, messagePipe[0],
0, ALOOPER_EVENT_INPUT, looperCallback, nullptr);
LOGI("fd is registered");
// send few messages from arbitrary thread
std::thread worker([]() {
for(char msg = 100; msg < 110; msg++) {
LOGI("send message #%d", msg);
write(messagePipe[1], &msg, 1);
sleep(1);
}
});
worker.detach();
}
// this will be called on main thread
static int looperCallback(int fd, int events, void* data) {
char msg;
read(fd, &msg, 1); // read message from pipe
LOGI("got message #%d", msg);
return 1; // continue listening for events
}
这段代码产生下一个输出:
This code produces next output:
06-28 23:28:27.076 30930-30930/? I/sergik: fd is registered
06-28 23:28:27.076 30930-30945/? I/sergik: send message #100
06-28 23:28:27.089 30930-30930/? I/sergik: got message #100
06-28 23:28:28.077 30930-30945/? I/sergik: send message #101
06-28 23:28:28.077 30930-30930/? I/sergik: got message #101
06-28 23:28:29.077 30930-30945/? I/sergik: send message #102
06-28 23:28:29.078 30930-30930/? I/sergik: got message #102
06-28 23:28:30.078 30930-30945/? I/sergik: send message #103
06-28 23:28:30.078 30930-30930/? I/sergik: got message #103
06-28 23:28:31.079 30930-30945/? I/sergik: send message #104
06-28 23:28:31.079 30930-30930/? I/sergik: got message #104
06-28 23:28:32.079 30930-30945/? I/sergik: send message #105
06-28 23:28:32.080 30930-30930/? I/sergik: got message #105
06-28 23:28:33.080 30930-30945/? I/sergik: send message #106
06-28 23:28:33.080 30930-30930/? I/sergik: got message #106
06-28 23:28:34.081 30930-30945/? I/sergik: send message #107
06-28 23:28:34.081 30930-30930/? I/sergik: got message #107
06-28 23:28:35.081 30930-30945/? I/sergik: send message #108
06-28 23:28:35.082 30930-30930/? I/sergik: got message #108
06-28 23:28:36.082 30930-30945/? I/sergik: send message #109
06-28 23:28:36.083 30930-30930/? I/sergik: got message #109
正如您从 pid-tid 对中看到的 - 消息在主线程上接收.当然,您可以发送比单字节消息更复杂的内容.
As you see from pid-tid pairs - messages are received on main thread. And of course you may send something more complicated than one-byte messages.
这篇关于Android JNI - 从 C++ 调用 Android UI 线程上的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android JNI - 从 C++ 调用 Android UI 线程上的函数
基础教程推荐
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01