2

in my Android app I have a native code that runs on separate thread, it use a while loop. The native code send data periodically to backend server using HTTP GET method. Now, I need the native code to also "send" data to my main Java UI thread.

My question is, how can I send some kind of an event or signal, that can be captured by the main Java UI thread, so that it knows when to perform some kind of action (updating the UI)? I know I can use JNI to call native code from Java side, but I do not know how to perform similar thing from native side to Java side.

Thanks guys!

bonchenko
  • 567
  • 1
  • 9
  • 21

1 Answers1

0

You can call any Java method from your native code as described here. In your case you'll need to use Activity's runOnUiThread() inside that method to run your code on the main thread.

Grishka
  • 2,465
  • 1
  • 20
  • 38
  • Hi Grishka. Would this means the java method run on the native thread? I really want the database and UI related codes to be run on the main thread, because I want to keep the native thread as fast as possible. – bonchenko Feb 02 '14 at 03:50
  • Yes, exactly. And why do you need to send requests from native code, not from Java? – Grishka Feb 02 '14 at 10:39
  • Thanks, great suggestion anyway, I am trying so that the native code only call a quick method in Java that change a simple variable. Then my main Java codes have a custom listener than listen to the variable and call the required codes. Because I do image processing in native code, it is faster in native code, but it is easier to access database and update UI in main Java code – bonchenko Feb 02 '14 at 11:22