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...


11 comments:

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

Unknown 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

Unknown said...
This comment has been removed by the author.
Unknown 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

Unknown 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

Unknown 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

shri said...

very good post...!
internship in chennai for ece students
internships in chennai for cse students 2019
Inplant training in chennai
internship for eee students
free internship in chennai
eee internship in chennai
internship for ece students in chennai
inplant training in bangalore for cse
inplant training in bangalore
ccna training in chennai


nisha said...

The blog is very impressive, every concepts are easily able to understand explained every concept should be very easy in the manner.

Data Science Training Course In Chennai | Data Science Training Course In Anna Nagar | Data Science Training Course In OMR | Data Science Training Course In Porur | Data Science Training Course In Tambaram | Data Science Training Course In Velachery

rocky said...

I'm read your website. I like most useful informative content.
Python Training in Chennai

Python Training in Bangalore

Python Training in Hyderabad

Python Training in Coimbatore

Python Training

python online training

python flask training

python flask online training


Jayalakshmi said...

Thank you for taking the time to discuss this informative content with us. I feel happy about the topic that you have shared with us.
data science training in chennai

data science training in tambaram

android training in chennai

android training in tambaram

devops training in chennai

devops training in tambaram

artificial intelligence training in chennai

artificial intelligence training in tambaram

praveen said...

I must appreciate you for providing such a valuable content for us. This is one amazing piece of article. Helped a lot in increasing my knowledge
data science training in chennai

data science training in porur

android training in chennai

android training in porur

devops training in chennai

devops training in porur

artificial intelligence training in chennai

artificial intelligence training in porur