Log in or register to post comments

problem invoking java activity class when object is tracked

May 3, 2011 - 12:35pm #1

Hi all,
I am facing problem when invoking java method from JNI. I want to trigger java activity class to display dialog when object is tracked as in the function:
Java_com_qualcomm_QCARSamples_ImageTargets_ImageTargetsRenderer_renderFrame(JNIEnv *env, jobject obj)
{
//some code here
for(int tIdx = 0; tIdx {
// Get the trackable:

jclass clazz = env->GetObjectClass(obj);
jmethodID getObjectTrackedMethodID = env->GetMethodID(clazz,"ObjectTracked", "()I");

env->CallIntMethod(obj, getObjectTrackedMethodID );
//some code here
}
}

at java ImageTargetsRenderer class:
static boolean invoked=false;
public int ObjectTracked()
{
if(!invoked)
{
invoked=true;
_objImageTargets.Test();
}

return 1;
}
at java ImageTargets class
public void Test()
{
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setMessage("This is an alert with no consequence");
dlgAlert.setTitle("App Title");
dlgAlert.setPositiveButton("OK", null);
dlgAlert.setCancelable(true);
dlgAlert.create().show();
}

after the object tracked, the application crashed & exit.
Please help me how to achieve that??

Re: problem invoking java activity class when object is tracked

May 4, 2011 - 8:40am #3

Thank you very very much.

Re: problem invoking java activity class when object is tracked

May 3, 2011 - 2:57pm #2

The renderFrame method is called in a different thread than the main/UI thread. You can only make UI changes in the UI thread. To solve this, use a Handler.

ImageTargets.java

private void initApplicationAR()
{
    // some code here

    mRenderer = new ImageTargetsRenderer();
    mGlView.setRenderer(mRenderer);

    mRenderer._alertDialogHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            Test();
        }
    };
}

ImageTargetsRenderer.java

public Handler _alertDialogHandler;
public int ObjectTracked()
{
    if(!invoked)
    {
        invoked=true;
        _alertDialogHandler.sendEmptyMessage(0);
    }

    return 1; 
}

- Kim

Log in or register to post comments