Send messages to android.os.Handler from JNI

670 views
Skip to first unread message

sfontene79

unread,
Jun 18, 2010, 10:08:07 AM6/18/10
to android-platform
Hi there,

I am developing a JNI layer where I create threads to perform some
operations that can take a long time. Now I'd like them to notify back
the java layer by sending messages to a android.os.Handler class
running there.

I was thinking of writing a private method in my Java class to send
the message and to call it from my JNI layer :

private void notifyThreadCompleted(byte[] result) {
Message msg = mMyHandler.obtainMessage();

// Build the message from result
(...)

mMyHandler.sendMessage(msg);
}

Is this correct to do so ? Is there any way to send the message
directly from JNI ?

Thanks,
_________
Sylvain

FrankG

unread,
Jun 21, 2010, 9:47:42 AM6/21/10
to android-platform
Hi Sylvain,

I'm confused why you will use android.os.Handler ?

But nevertheless : Yes, you can make a kind of callback from you
native ndk stuff.

Simply provide a public call in your own "JNI-Handler" class i.e.

public void onEvent(int event) {
..
}

and in your Thread ( C-Code example )
// as a kind of pseudo code without error handlng
int event_mask; // your event-mask
jclass cls;

cls = (*env)->FindClass(env, "<yourpackage>/<yourhandler>");
jmethodID mid = (*env)->GetMethodID(env, cls, "onEvent", "(I)V");

(*env)->CallVoidMethod(env, obj , mid ,event_mask);

And your "Handler" calls you can use the Android Handler in the normal
way.

Good luck ! Frank
Reply all
Reply to author
Forward
0 new messages