{Apologies for the cross-post with android-developers forum. Haven’t received any answers there}
I have an interesting design challenge:
I have a frontend(Activity) and a backend (written in native C/C++)
code. The backend is a complex object which partially controls the
flow of application & once started runs in it’s own thread. So I have
a “distributed control” scenario.
The Activity needs to be able to send messages asynchronously to the
backend which then takes certain actions. But the backend also needs
to be able to send messages asynchronously to the Activity, to which
it responds by chnaging UI, firing methods etc.
Essentially what I need is a two-way listener.
So backend sends a message to screen (take picture, prompt user, get
location, take another picture now etc) and screen does what it needs
to do. But in addition, the screen should also be able to call the
backend-listener to send back messages (captured camera image, system
generated – “I got paused/destroyed” message, etc) in the callbacks of
these events. Main problem is that this is all asynchronous.
Is this possible without tight-coupling? Is this even possible?
I have thought of Asynctask/handlers (but that’s a one way street for
informing the UI thread), observer-pattern (both objects will be
observer/observable?) but confused on where to begin. Any thoughts,
links would be very helpful.
Within your native code, you can use JNI to obtain classes (and objects) from your VM, and once you’ve got a class (or object) you can locate methods and call them (static methods for a class, all methods for an object). It seems to me that the simple solution is to provide a helper in your java class which encapsulates the native methods and have the native code call into it.
In the past, I’ve needed to have native code determine if its thread has been interrupted at the Java level. Java provides java.lang.Thread.currentThread() as a static to find your own thread, and java.lang.Thread.isInterrupted() to [non-destructively] determine the interrupted status. I used the following to solve this problem at the native level; perhaps you can use it towards your needs (with appropriate adaptation to message sending, of course):
Instantiation is based on the JNIEnv * provided to your native method, and a simple allocate/call/deallocate line of code to call the isInterrupted() method is: