Callback mechanism from native to java ??

5,667 views
Skip to first unread message

Kapil

unread,
Sep 17, 2009, 2:53:18 AM9/17/09
to android-ndk
Hi,
I am porting my C application in Android where I am reusing all
my native code via NDK.My native function (which is called from Java)
is creating a thread.
Is there any callback mechanism to access Java/Android
from my "native" thread ??

Thanks
Kapil

Raji Reddy Kandi

unread,
Sep 17, 2009, 10:27:05 AM9/17/09
to andro...@googlegroups.com
 
Hi,
 
What do you mean callbacks?
 
1. you can call any method of the Java class which has setup the native.
2. you can even access any android managed service (i.e Java remotable object which is already added to ServiceManager) from native layer.
 
If not clear, lets discuss more...

Regards,

Kapil

unread,
Sep 17, 2009, 11:04:54 AM9/17/09
to android-ndk
Hi,
Thanks for reply.

From Callback I mean Can I call any Android Method from native or can
I launch any Anroid Activity from my native code.
Flow Of my App will be :
1) Start->Anroid Activity -> JNI call -> native funtion -> native
thread ...

2 ) This Native thread will do some backend processing asynchronously
& once the that is done I need to give some data to android & launch
one of the activity ...

Problem lies in point (2) ..
I have no idea how to do it ..

Thanks
Kapil

Raji Reddy Kandi

unread,
Sep 17, 2009, 11:10:46 AM9/17/09
to andro...@googlegroups.com
1) Start->Anroid Activity ->your_java_class -> JNI call -> native funtion -> native
thread ...

then, if u want to launch any other android activity, let your_java_class do it for you....
 
so,
 
your_c++_calls -> JNI ->your_java_class ->new activity.....
 
 
Regards,

Sridhar M

unread,
Sep 18, 2009, 2:22:56 AM9/18/09
to andro...@googlegroups.com
This link gives a comprehensive way of how callbacks into JAVA are done from native lib.

http://android.wooyd.org/JNIExample/files/JNIExample.pdf 

-Sridhar

David Given

unread,
Sep 18, 2009, 9:32:15 AM9/18/09
to andro...@googlegroups.com
Kapil wrote:
[...]

> Is there any callback mechanism to access Java/Android
> from my "native" thread ??

Call vm->AttachCurrentThread() to tell the JVM that your new native
thread exists, and then you can make JNI method calls in the usual
manner for it. (But remember to call vm->DetachCurrentThread() before
you terminate the thread, and that any Java methods called will run from
your new thread and not the Android UI thread.)

--
┌─── dg@cowlark.com ───── http://www.cowlark.com ─────

│ "They laughed at Newton. They laughed at Einstein. Of course, they
│ also laughed at Bozo the Clown." --- Carl Sagan

fadden

unread,
Sep 18, 2009, 2:00:16 PM9/18/09
to android-ndk
On Sep 17, 11:22 pm, Sridhar M <sridhar....@gmail.com> wrote:
> This link gives a comprehensive way of how callbacks into JAVA are done from
> native lib.
>
> http://android.*wooyd*.org/*JNI*Example/files/*JNI*Example.pdf

(I removed the asterisks from the URL.)

The stuff on page 8 suggests some confusion about caching class
objects in local references, as well as the finer points of FindClass
and class loaders (which is a bit complicated). I'm also not a fan of
perpetually attaching and detaching native threads unless it's
happening infrequently, so I don't know how closely I'd follow the
callback model.

It uses android::AndroidRuntime::registerNativeMethods(), which isn't
part of the NDK. Of course, this was written before the NDK was
released, so that's understandable. (The JNI RegisterNatives function
should be used.)

This is another instance of someone thinking JNI_OnLoad is mandatory.
(I think I'd better re-phrase the debug message that's displayed when
it's not there -- too many people have made this mistake.) JNI for
Android is like JNI for any other VM.

In general the document is interesting, but some of the commentary is
misleading, and I think most people will be better served by samples
from the Android source tree (especially since the last page of the
doc points out that the examples cause the app to crash if you hit the
"back" button and then restart the activity).

As usual, I recommend reading the JNI spec and some of the Android
sample code, and then carefully reading the JNI Tips document.

http://android.git.kernel.org/?p=platform/dalvik.git;a=blob_plain;f=docs/jni-tips.html;hb=HEAD

Rajesh Yadaav

unread,
Sep 6, 2013, 11:01:30 AM9/6/13
to andro...@googlegroups.com
as according krr_android:

 
your_c++_calls -> JNI ->your_java_class ->new activity.....
works well for me but calling
Android app(Activity,Service,Handler..)/content(Toast) cause  crash the android application...

I am trying to use ActivityClass.this.runOnUThread(runnable).. while getting control in java method which is invoked from C++ method.

AFAIK android Android application component like Activity,Service Handler and other  runs on Android main thread.. and may be I am trying to call android components from some worker thread.


the exception i am getting is this :
W/dalvikvm( 4425): threadid=12: thread exiting with uncaught exception (group=0x419586f0)
E/AndroidRuntime( 4425): FATAL EXCEPTION: Thread-514
E/AndroidRuntime( 4425): java.lang.NullPointerException
E/AndroidRuntime( 4425):        at com.example.extension.Dialer.onAnswerCall(Dialer.java:86)
E/AndroidRuntime( 4425):        at dalvik.system.NativeStart.run(Native Method)
W/ActivityManager(  256):   Force finishing activity com.example.extension/.Dialer





with this Exception...
Reply all
Reply to author
Forward
0 new messages