Например, события пользовательского интерфейса, такие как нажатие клавиши или перетаскивание, поступают в основной теме. Чтобы обработать их, я пересылаю эти события в C ++ через JNI. Иногда определение действия, которое нужно предпринять, может быть трудоемким, поэтому я не хочу блокировать основной поток. ОС, как правило, ожидает, что такая работа будет выполнена на фоновой потоке. /> Вот упрощенная версия того, что я имею сейчас: < /p>
// Kotlin side (UI thread)
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
// Forward event to C++ via JNI
return NativeBridge.onKeyEvent(keyCode, event.action)
}
< /code>
// C++ side
bool onKeyEvent(int keyCode, int action) {
// This may take time...
processEvent(keyCode, action); // So, I want to make this call go async...
return ? // Here, is where I am confused
}
< /code>
How should I structure this event dispatching (Java → JNI → C++ → background thread) so that:
- The UI thread isn’t blocked.
- Events are always processed in the same order they were received.
If I return false, the OS also updates the text.
If I return true, the OS does nothing, and I’d need to manually update the text.
If I process these events asynchronously, I can’t immediately decide whether to return true or false.
This seems to force me into manual string management, which I’d like to avoid if possible. Is there a recommended way to handle such cases?
Подробнее здесь: https://stackoverflow.com/questions/797 ... d-but-keep
Мобильная версия