Calling Qt C++ from Android java (QtActivity)

2,284 views
Skip to first unread message

deion

unread,
Sep 25, 2012, 8:36:33 AM9/25/12
to andro...@googlegroups.com
Hi,

I managed to call Android java code from Qt code using JNI and the few blog posts regarding this issue on alpha4.

However now I am in the QtActivity java method called from Qt and I need to pass back some result to the Qt C++ code that initially called the Java part part.
How can this be done? Can someone please point to some resources regarding this?

Thanks & regards,
Ionut

paulus

unread,
Sep 25, 2012, 9:09:23 AM9/25/12
to andro...@googlegroups.com
Since alpha4 i wouldn't call the JNI functionality an issue anymore as it has been fixed properly. Also note that the JNI functionality is a C++\Java thing and is separate from Qt.


Although I wrote (hacked) it a while ago and I'm not very experience with JNI anyway, I call a Java method called 'GetDataDirectory()' from my C++ code and it returns a string to the caller. A snippet:

jclass appClass = env->GetObjectClass(classPointer);
if (appClass){
    jmethodID methodID = env->GetMethodID(appClass, "GetDataDirectory", "()Ljava/lang/String;");
    jstring result = (jstring)env->CallObjectMethod(classPointer, methodID);

    const char *strResult = env->GetStringUTFChars( result, 0 );

    // do something useful here, if you're going to store the result I suggest you copy the return value

    env->ReleaseStringUTFChars( result, strResult);
}



deion

unread,
Sep 25, 2012, 9:11:11 AM9/25/12
to andro...@googlegroups.com
Correction:

I am in onActivityResult() method of the QtActivity java class and I need to call some callback function back on the Qt side; It could have been understood from the initial post that I needed to return
data from the originall java method synchronously which is trivial through the return value.

deion

unread,
Sep 25, 2012, 9:22:30 AM9/25/12
to andro...@googlegroups.com
To summarize:

Qt code calls QtActivity.java method ; here I create an intent to start the native Android photo gallery activity and then immediatelly return from the java function;
Now the user selects a photo and when done the Java method QtActivity.onActivityResult() gets the URL String of the selected photo; Now I need to return this URL String back to the Qt side.


How can this be done?

Thanks Paulus for your response.

paulus

unread,
Sep 25, 2012, 9:29:22 AM9/25/12
to andro...@googlegroups.com
Well you're original post talked about 'pass back some result' which primarily refers to returning a value in my book. When talking about callback functions I always try to avoid using the word return altogether. That being said, take a look here: http://www.velocityreviews.com/forums/t145122-transfering-callback-function-to-jni.html

paulus

unread,
Sep 25, 2012, 9:31:45 AM9/25/12
to andro...@googlegroups.com
Looking at the post times, I think we've been communicating asynchronously here :) Regarding callbacks I couldn't find much for Java -> C++ (everything seems to be the other way around), however the link in my last post seems to appear somewhat useful.

deion

unread,
Sep 25, 2012, 9:51:07 AM9/25/12
to andro...@googlegroups.com
This is finally a synchronous reply, but hopefully not blocking :)

Looking at the ultimate Necessitas JNI reference ( that is the last post here: https://groups.google.com/forum/?fromgroups=#!topic/android-qt/D7XPE7t9RMo ) I think the key word here is jniRegisterNativeMethods and I will be able to define native methods in the QtActivity java class that I will implement on the c++ side, and thus be able to communicate Java->C++, at least that's the theory ...

Thanks,
Ionut

paulus

unread,
Sep 25, 2012, 10:07:04 AM9/25/12
to andro...@googlegroups.com
Ah so you're not really needing callbacks in the classic sense? I was assuming you wanted your C++ code to somehow pass a C++ function pointer to the Java side such that it can be called later from Java back to C++. I'm not even sure this is possible, but as stated my JNI experience is very limited at the least.
If this happens to be what you need you could work around the problem by keeping the callback stuff on the C++ side. By this I mean that when you call the Java code you generate and pass it a token. On the C++ side you would store this token and any function pointers. Later, when the Java side wants to do a callback you simply call a general C++ function as usual with JNI and pass it the token + parameters. The C++ code could then retrieve the stored callback based on the token and call it using the provided parameters.

If you just want to call C++ methods from within Java (and they are known at compile time) then I would certainly refer you to the JNI article I posted earlier here.

Good luck!

Ray Chan

unread,
Oct 12, 2012, 4:04:45 AM10/12/12
to andro...@googlegroups.com
  I'm also looking forward to the solution ! Almost the same scenario as yours.

在 2012年9月25日星期二UTC+8下午9时22分30秒,deion写道:
Reply all
Reply to author
Forward
0 new messages