QtAndroidExtras - call Qt from Java
-
wrote on 21 Nov 2013, 09:37 last edited by
Hi,
i have a small test function which does the following:
@
QString tmp = "def";
if (QAndroidJniObject::isClassAvailable("java/lang/String")) {
tmp = "string";
}
if (QAndroidJniObject::isClassAvailable("org/qtproject/qt5/android/QtNative")) {
tmp = "qt5";
}
if (QAndroidJniObject::isClassAvailable("com/somePackage/JniTester")) {
tmp = "demo";
}
@
The first two tests are successfully executed, but the third line crashes with a segmentation fault.The file JniTester located in android-sources/src/com/somePackage/JniTester.java looks like this:
@
package com.somePackage;public class JniTester extends org.qtproject.qt5.android.bindings.QtActivity {
public static int test() { return 17; }
}
@The project file contains these lines next to the files:
@
ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android-sources
ANDROID_PACKAGE = com.somePackage
ANDROID_MINIMUM_VERSION = 16
ANDROID_TARGET_VERSION = 16
ANDROID_APP_NAME = Qt TestTARGET = MyTest
TEMPLATE = app
@What do i have to add to find the class and call methods?
Regards
-
wrote on 21 Nov 2013, 09:54 last edited by
Ok, now i removed the derivation and after several rebuilds and deleting of the build-directory the three tests are successful.
-
wrote on 21 Nov 2013, 13:31 last edited by
but it keeps crashing if i add
@
if (QAndroidJniObject::isClassAvailable("android/bluetooth/BluetoothAdapter")) {
tmp = "bluetooth";
}
@
How can i specify an AndroidManifest file for my java file. I could imagine that missing permissions might be the problem. But i have no idea how to specify one.
The documentation is very short and does not go into detail on these parts...
Thank youedit: I test on an 4.1.2 device. But the same call works on an 4.2.2. That is very strange....
-
wrote on 22 Nov 2013, 14:36 last edited by
Ok,
now I've made some progress. The difficulties caused by different android versions may stem from the bluetooth stack which was exchanged from 4.1 to 4.2.
Now my problem is to supply data from the java class back to my qt app.
Here is a code snippet
@
//Test.java
package de.somePackage.Test;imports....
public class Test {
private native void processData(int x);
int getSomething() {
//here is a thread started which calls
processData(23);
}
}//constructor: QAndroidJniEnvironment env, QAndroidJniObject m_tobj
JNINativeMethod methods[] {{"processData", "(I)V", reinterpret_cast<void*>(&MainWindow::processData)}}; m_tobj = QAndroidJniObject("de/somePackage/Test"); jclass objectClass = env->GetObjectClass(m_list.object<jobject>()); env->RegisterNatives(objectClass, methods, sizeof(methods) / sizeof(methods[0])); env->DeleteLocalRef(objectClass);
void MainWindow::someFunc() {
jint k = m_tobj.callMethod("getSomething", "(V)I");
}void MainWindow::processData(JNIEnv *env, jobject thiz, jint x) {
Q_UNUSED(env)
Q_UNUSED(thiz)
int k = x;
QString mytmp = "dudu";
}
@
getSomething() is executed, also processData is called, but the argument does not contain the 23, just some memory garbage (although its the same number each time).
But what changes each call is the pointer to env, what is really strange.Any ideas why the parameter is not correclty supplied?
Regards
-
wrote on 24 Nov 2013, 09:20 last edited by
no ideas?
-
wrote on 25 Nov 2013, 13:40 last edited by
ok, made a little step.
If i declare processData static everythink works. Now i just need to figure out how to use non-static functions. -
wrote on 28 Nov 2013, 13:52 last edited by
Post here solution pls, if u will find the siolution )))
-
wrote on 29 Nov 2013, 15:21 last edited by
You need to pass a function pointer from an instance of the MainWindow.
-
wrote on 2 Dec 2013, 16:33 last edited by
I found this http://doc-snapshot.qt-project.org/qt5-stable/qtandroidextras-notification-example.html
but I couldn't make it work
-
wrote on 2 Dec 2013, 21:51 last edited by
The example show how to call Java from Qt. And do you need to call Qt from Java, right?
3/10