Call java method from C ++ to qt

I am trying to call a method defined in android activity in C ++ qt using QAndroidJniObject. here is my call in c ++ class

QAndroidJniObject data =  QAndroidJniObject::callStaticObjectMethod("com/android/app/appActivity",
                                                                      "appData",
                                                                      "(I)Ljava/lang/String;");
QString dataValue = data.toString();
qDebug() <<"Data is " << dataValue;

this appData is defined in the andAndroid class appActiviy and returns the string this is the specific method that I want to call and get the returned string value

static  String appData(){
    Log.d("App Data is ", "Working");
    return data;
}

but I get null - this is dataValue, and that also doesn't cause errors.

+3
source share
3 answers

Thanks guys for your answer, finally I figured it out. It was simple, then I tried

QAndroidJniObject data =  QAndroidJniObject::callStaticObjectMethod("com/android/app/appActivity",
                                                                  "appData",
                                                                  "(I)Ljava/lang/String;");

In this code, I did not know that this (I)Ljava/lang/String;means the type of parameter that your Java method accepts, but in my case it was not. So the correct answer

QAndroidJniObject data =  QAndroidJniObject::callStaticObjectMethod<jstring>("com/android/app/appActivity",
                                                                  "appData")`

java-. , ...

0

, Java.

Qt:

Java

Java, , , , , .

. JNI .

void functionException()
{  
    QAndroidJniObject myString = QAndroidJniObject::fromString("Hello");  
    jchar c = myString.callMethod<jchar>("charAt", "(I)C", 1000);  
    QAndroidJniEnvironment env;
    if (env->ExceptionCheck()) {
        // Handle exception here.
        env->ExceptionClear();
    }
}

com/android/app/appActivity, com/android/app/Activity?

+3

:

  • Log.d() , , ?

  • , , , Java . callStaticObjectMethod().

  • As already mentioned by Alex P, exceptions must be handled, or they will give you a headache, as they can happen quite often and the whole application crashes.

  • I can not find any class in com / android / app / appActivity in the Android documentation. Did you mean com / android / app / Activity? If so, I cannot find a method named "appData" here .

+2
source

All Articles