本文介绍了Android的:如何实现"分布式控制"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

{道歉交叉后与Android的开发者论坛。没有收到任何答复有}

我有一个有趣的设计挑战:

I have an interesting design challenge:

我有一个前端(活动)和后端(用本机C / C ++)code。后端是一个复杂的对象,部分控制流应用程序和放大器;一旦开始运行在它自己的线程。所以,我有一个分散控制的情景。

I have a frontend(Activity) and a backend (written in native C/C++)code. The backend is a complex object which partially controls theflow of application & once started runs in it's own thread. So I havea "distributed control" scenario.

活动需要能够发送消息异步的后端然后采取一定的行动。但在后端还需要要能够发送消息异步的活动,对此它响应由chnaging UI,烧制方法等。

The Activity needs to be able to send messages asynchronously to thebackend which then takes certain actions. But the backend also needsto be able to send messages asynchronously to the Activity, to whichit 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, getlocation, take another picture now etc) and screen does what it needsto do. But in addition, the screen should also be able to call thebackend-listener to send back messages (captured camera image, systemgenerated - "I got paused/destroyed" message, etc) in the callbacks ofthese events. Main problem is that this is all asynchronous.

这是可能的,而不紧耦合?这甚至可能吗?

Is this possible without tight-coupling? Is this even possible?

我曾想过的AsyncTask /处理程序(但是这是一个单行道的通知UI线程),观察者模式(两个对象将是观察者/可观察到的?),但困惑于哪里开始。有什么想法吗,链接将是非常有益的。

I have thought of Asynctask/handlers (but that's a one way street forinforming the UI thread), observer-pattern (both objects will beobserver/observable?) but confused on where to begin. Any thoughts,links would be very helpful.

推荐答案

在您的本地code,可以使用JNI来从你的虚拟机获得类(和对象),一旦你已经有了一个类(或对象),你可以找到方法,并呼吁他们(静态方法的一类,对对象的所有方法)。在我看来,这个简单的解决方案是提供您的Java类中的帮手,它封装了本地方法,并有天然code调用了进去。

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.

在过去,我需要有本土code确定如果线程处于Java级别被中断。 Java提供java.lang.Thread.currentThread()作为静态找到你自己的线程,并java.lang.Thread.isInterrupted()为[非破坏性]确定中断状态。我用下面的解决这个问题的母语水平;或许你可以用它全面满足客户需求(同时适当照顾到的信息发送,当然):

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):

/* JavaThread: this class is a simple wrapper to be used around    */
/* JNI's 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's 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's 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;
};

实例化是基于JNIEnv的*提供给您的本地方法,和一个简单的分配/电话/的code DEALLOCATE行调用isInterrupted()方法是:

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:

if (JavaThread(env).isInterrupted()) { ... }

这篇关于Android的:如何实现"分布式控制"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-21 00:45