@Kostya: Thanks for your answer. Based on your answer, Handler does
sound better that AsyncTask for this. I looked a little bit into
HandlerThread, probabaly have to try it out to understand it fully.

One question though, forgetting about the whole native thing for a
moment... even if there are two Java classes (one Activity & one
backend) with methods running in two threads. Can they BOTH implement
listeners, hold a reference to each other & call each other's
listening methods? That would make the design really tightly-coupled.
Is there a design pattern to use where they both can listen to each
other & call each other asynchronously - whenever needed to?

On May 26, 1:07 pm, Kostya Vasilyev <kmans...@gmail.com> wrote:
> I would start by wrapping some of the C++ code in Java with JNI (you
> probably already have that).
>
> AsyncTask isn't really appropriate, because it's a task, not a process,
> meaning it doesn't have a duration in time any more than necessary for one
> item of work.
>
> You can use Handler objects, they are not necessarily tied to the UI. You'd
> need two: one on the UI thread owned by the activity, one owned by the Java
> wrapper of the back-end thread, and associated with that thread.
>
> Take a look at HandlerThread, that's the basic idea for your backend, except
> you'd be calling a native method for processing messages.
>
> -- Kostya
>
> 2011/5/26 Oceanblue <ns1...@gmail.com>
>
> > 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 nowm 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.
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Android Developers" group.
> > To post to this group, send email to android-developers@googlegroups.com
> > To unsubscribe from this group, send email to
> > android-developers+unsubscr...@googlegroups.com
> > For more options, visit this group at
> >http://groups.google.com/group/android-developers?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to