I made an image processing application using OpenCV and Android NDK. Now, I want to display the result in my main activity which is the dashboard containing some data and graphs based on the native image processing application.
I look around and found that native code is only available to be run as activity (NativeActivity class), which is my approach currently where the main activity is replaced by blank screen of the native activity reference#1.
My questions are, is this true? How can I run my native code from the main activity while keeping the main activity active in foreground and the native code run in background?
Thanks guys!
To specify the thread on which to run the action, construct the Handler using a Looper for the thread. A Looper is an object that runs the message loop for an associated thread. Once you've created a Handler , you can then use the post(Runnable) method to run a block of code in the corresponding thread.
I look around and found that native code is only available to be run as activity
No. Any Java class can have native methods in Android. You can have a background worker thread, implemented either as a Thread
-derived class or as a Runnable
, which would do the background work by calling native method(s).
pthreads are another possiblity, but those threads are invisible to the Java subsystem; you might want to call Java code from the worker thread - at the very least to pass something back to the UI thread. It's easier if the worker thread was started in Java in the first place.
For the record, Android services are not threads. They run on the main thread. Threads can be started from a service, but they can be started from an activity as well. Thread's lifetime is up to you.
The NativeActivity stuff is for having completely Java-less apps. You don't have to follow that way.
Android supports pthreads to a large extent. Feel free to use pthread_create()
and her kin in your code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With