QtAndroidExtras - call Qt from Java
-
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
-
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....
-
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
-
Post here solution pls, if u will find the siolution )))
-
I found this http://doc-snapshot.qt-project.org/qt5-stable/qtandroidextras-notification-example.html
but I couldn't make it work