Android: How to implement "distributed control"

{Apologies for the junction with the Android Developer Forum. There were no answers}

I have an interesting design task:

I have an interface (Activity) and a backend (written in my native C / C ++) code. Backend - it is a complex object, which partly controls the flow of the application and once it launches its own thread. So I have a "distributed control" scenario.

The operation should be able to send messages asynchronously. backend, which then takes certain actions. But backend also need to send an asynchronous message to the activity to which he responds by using the UI, shooting techniques, etc.

In fact, I need a double-sided listener.

Thus, the backend sends a message on the screen (remove the image, help the user to get a location, make another image, etc.), and the screen does what he needs to do. But, in addition, the screen must also be able to call the backend-listener to send messages (captured camera image, the system generated - the message "I got paused / destroy", etc.) in the event callbacks. The main problem is that all this asynchronously.

Is this possible without the hard link? Is it possible?

I thought about Asynctask / handlers (but it's a one-way street for informing on the UI thread), the observer pattern (both objects are the observer / observed?), But it confused with, where to start. There are suggestions, links would be very useful.

+4
source share
2 answers

Within your own code, you can use the JNI for classes (and objects) to your virtual machine, and after you have a class (or object), you can find methods and call them (static methods for a class, all methods for object). It seems to me that the simplest solution is to provide an assistant in your class java, which encapsulates its own methods and has it own code.

In the past I had to define your own code if its flow was interrupted at the Java level. Java provides java.lang.Thread.currentThread () as a static, to find their own flow and java.lang.Thread.isInterrupted () - [non-destructive] determines the interrupted status. I used the following to solve this problem on its own level; maybe you can use it for their needs (with a corresponding adjustment to send a message, of course):

/* JavaThread: this class is a simple wrapper to be used around */ /* JNI Thread class. It locates the provided functions as needed */ /* and when it is destroyed (such as going out of scope) it will */ /* release its local references. */ class JavaThread { public: JavaThread(JNIEnv *env) { mEnv = env; /* find the Java Thread class within the JVM: */ mThread = mEnv->FindClass("java/lang/Thread"); /* find the Thread.currentThread() method within the JVM: */ mCurrentThreadMID = mEnv->GetStaticMethodID(mThread, "currentThread", "()Ljava/lang/Thread;"); /* find the current thread isInterrupted() method: */ mIsInterruptedMID = mEnv->GetMethodID(mThread, "isInterrupted", "()Z"); } ~JavaThread() { if (mThread) { mEnv->DeleteLocalRef(mThread); mThread = 0; } } bool isInterrupted() { bool bResult; if (!mThread) return false; if (!mIsInterruptedMID) return false; /* find the current thread (from the JVM perspective): */ jobject jCurrentThread = (jobject)mEnv->CallStaticObjectMethod(mThread, mCurrentThreadMID); if (NULL == jCurrentThread) return false; /* see if the current thread is interrupted */ bResult = (bool)mEnv->CallBooleanMethod(jCurrentThread, mIsInterruptedMID); /* delete the current thread reference */ mEnv->DeleteLocalRef(jCurrentThread); /* and return the result */ return bResult; } private: JNIEnv *mEnv; jclass mThread; jmethodID mCurrentThreadMID; jmethodID mIsInterruptedMID; }; 

Activation is based on JNIEnv *, provided your own method, and a simple string selection / call / release numbers for calling the isInterrupted ():

 if (JavaThread(env).isInterrupted()) { ... } 
+2
source

How about using a blocking queue, because your description reminds me of the classic producer-consumer problem from the class of parallel programming.

http://en.wikipedia.org/wiki/Producer-consumer_problem

In addition, Java 1.5, it seems, is the realization of a blocking queue, I can not remember my head, if it is available on Android.

http://download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/BlockingQueue.html

I would suggest that you can set up 2 unidirectional blocking queue to communicate in any way. Q1 will have a front-end as a producer and as a back-end consumer. Q2 will have a front-end interface as the user and the server the server as a producer.

You can also use the "Command" pattern design for messages "messages".

http://en.wikipedia.org/wiki/Command_pattern

NTN

0
source

All Articles