Tuesday, April 15, 2014

Android JNI Call back


In the above images shows all possible scenarios of Android JNI calls and its call backs.

Android UI mostly implemented in Java, then if needed there will be C++ implementation to  perform low level operations. In this case JNI is used.

1. Java to C++ calls :
First UI will need to make some call to C++, for example start engine etc.

In Java:
1.1. Need to write a native method:
package demo;
class Service{
     public static native doSomething();
}
1.2. Call this method to invoke/access the low level.
....
//Somewhere
Service.doSomething();
....

In C++:
1.2. Need to write the implementation for above native method:
JNIEXPORT jint JNICALL Java_demo_Service_doSomeThing(JNIEnv * env, jobject ){
..............
..............
}

2. C++ to Java Call
There may be some cases C++ Implementation may need to call back UI, for example inform the progress update.
This can happen from the same thread which the initial called made from Java or C++ may have created a new thread and may call from there.

In Java: Write a Observer to receive those call backs.
package demo;
class Service{
     public static void onCallBack(){
          ............
          //add implementation to handle call back
          ...............
     }
}

Any callback from C++ to Java need to be implemented using given JNI refection APIs.

jclass jcls = env->FindClass("dem/Service");
jmethodID mid = env->GetStaticMethodID(jcls,"onCallBack","....");
env->CallStaticVoidMethod(jcls,mid);

This will perform what we want, but we will need some more extra implementation to make it work in caseof concurrent access from c++.

2.1 Calling from same thread.
In this case, the above listing will work without any issue. We can use the same JNIEnv which passed in Java_demo_Service_doSomeThing call above 1.2

2.2 Calling from other thread 
In this case we cannot use the above env passed with Java_demo_Service_doSomeThing   as it is invalid in other threads.
So We have to create a new JNIEnv using JavaVM, which is passed in JNI_OnLoad.
So,
2.2.1 Write a JNI_OnLoad in your native c++ code:

static JavaVM * s_Jvm;

JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
        s_Jvm = vm;//Keep for future use.
return JNI_VERSION_1_6;

}

2.2.2 Attach Current Thread to create JNIEnv:
JNIEnv * env;
s_Jvm->AttachCurrentThread(&env,NULL);

2.2.3 If you try FindClass with this JNIEnv it will return NULL in Android since uses special Class Loader in Java and in New thread which created in C++ will use the

jclass jcls = env->FindClass("dem/Service");

2.2.4 So this should be done inside OnLoad method : (code are in bold) and assign to global reference to use in future.

jclass s_jcls;

JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
        s_Jvm = vm;//Keep for future use.
        s_jcls =  env->NewGlobalRef(env->FindClass("dem/Service"));
return JNI_VERSION_1_6;

}

2.2.5 Now you can use this s_jcls to make any call backs

GetStaticMethodID
CallStaticObjectMethod

etc...


10 comments:

mary Brown said...

Thanks for the post, I am techno savvy. I believe you hit the nail right on the head. I am highly impressed with your blog. It is very nicely explained. Your article adds best knowledge to our Java Online Training from India. or learn thru Java Online Training from India Students.

ganga besant said...

It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
Block Chain Training in annanagar

Block Chain Training in pune

Block Chain Training in velachery

john kevin said...

Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.

rpa training in chennai | best rpa training in chennai | rpa training institute in chennai | rpa courses in chennai | rpa training in pune | rpa online training | rpa training in bangalore

john kevin said...
This comment has been removed by the author.
john kevin said...

Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.


rpa training in chennai | best rpa training in chennai | rpa training in chennai | rpa training in bangalore
rpa training in pune | rpa online training

john kevin said...

Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.


rpa training in chennai | best rpa training in chennai | rpa training in chennai | rpa training in bangalore
rpa training in pune | rpa online training

Ezhil K S said...

Nice post. By reading your blog, i get inspired and this provides some useful information. Thank you for posting this exclusive post for our vision. 
Data Science training in rajaji nagar | Data Science with Python training in chenni
Data Science training in electronic city | Data Science training in USA
Data science training in pune | Data science training in kalyan nagar

janani said...

It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
java training in chennai | java training in bangalore


java training in tambaram | java training in velachery

sai said...

Nice information, valuable and excellent design, as share good stuff with good ideas and concepts, lots of great information and inspiration, both of which I need, thanks to offer such a helpful information here.
python training in tambaram
python training in annanagar
python training in jayanagar

johnsy sai said...

I read this post two times, I like it so much, please try to keep posting & Let me introduce other material that may be good for our community.
Best Devops Training in pune
Devops interview questions and answers