Call Java method from c++
Solved
Mobile and Embedded
-
wrote on 29 Dec 2021, 18:33 last edited by mrdebug
Hi I need to call from c++ this java method:
public String GetScanResultAt(int count) { return "123"; }
I'm using this to call a java method without arguments that returs a string:
qDebug() << QAndroidJniObject(QtAndroid::androidActivity().callObjectMethod<jstring>("GetScanResultAt")).toString();
Which is the right way to change the line of code above to pass a int parameter?
-
Hi I need to call from c++ this java method:
public String GetScanResultAt(int count) { return "123"; }
I'm using this to call a java method without arguments that returs a string:
qDebug() << QAndroidJniObject(QtAndroid::androidActivity().callObjectMethod<jstring>("GetScanResultAt")).toString();
Which is the right way to change the line of code above to pass a int parameter?
wrote on 30 Dec 2021, 09:51 last edited by KroMignon@mrdebug said in Call Java method from c++:
Which is the right way to change the line of code above to pass a int parameter?
You have to add function signature:
qDebug() << QAndroidJniObject(QtAndroid::androidActivity().callObjectMethod<jstring>("GetScanResultAt", "(I)Ljava/lang/String;", jint(55)).toString();
-
wrote on 31 Dec 2021, 09:28 last edited by mrdebug
Below examples on how to use java methods with arguments
Java
public int Test01() { System.out.println("Test01"); return 0; } public int Test02(int Value) { System.out.println("Test02"); System.out.println(Value); return 0; } public int Test03(String Value) { System.out.println("Test03"); System.out.println(Value); return 0; } public String Test04(int Value) { System.out.println("Test04"); System.out.println(Value); return "Hello!!!"; } public String Test04b(String Value) { System.out.println("Test04b"); System.out.println(Value); return "Hello!!!"; } public String[] Test05(String Value1, String Value2) { System.out.println("Test05"); System.out.println(Value1); System.out.println(Value2); String[] Result= new String[2]; Result[0]= "Hello"; Result[1]= "world!!!"; return Result; }
Qt / C++
qDebug() << "Test01 result:" << QtAndroid::androidActivity().callMethod<jint>("Test01"); qDebug() << "Test02 result:" << QtAndroid::androidActivity().callMethod<jint>("Test02", "(I)I", 10); QAndroidJniObject Value1= QAndroidJniObject::fromString("Value1"); QAndroidJniObject Value2= QAndroidJniObject::fromString("Value2"); qDebug() << "Test03 result:" << QtAndroid::androidActivity().callMethod<jint>("Test03", "(Ljava/lang/String;)I", Value1.object<jstring>()); qDebug() << "Test04 result:" << QAndroidJniObject(QtAndroid::androidActivity().callObjectMethod("Test04", "(I)Ljava/lang/String;", 123456)).toString(); qDebug() << "Test04b result:" << QAndroidJniObject(QtAndroid::androidActivity().callObjectMethod("Test04b", "(Ljava/lang/String;)Ljava/lang/String;", Value1.object<jstring>())).toString(); QAndroidJniObject stringArray= QtAndroid::androidActivity().callObjectMethod("Test05", "(Ljava/lang/String;Ljava/lang/String;)[Ljava/lang/String;", Value1.object<jstring>(), Value2.object<jstring>()); if (stringArray.isValid()) { jobjectArray objectArray= stringArray.object<jobjectArray>(); for (int count= 0; count< env->GetArrayLength(objectArray); count++) { QAndroidJniObject AndroidJniObject= env->GetObjectArrayElement(objectArray, count); qDebug() << AndroidJniObject.toString(); } }
3/3