QT Mobile Application Development (vii): QML interaction with Java
The previous article talked about how to implement QML and C + + interactions, QML and C + + have a lot of interaction methods, respectively, using context variables, registering QML classes, and registering QML Singleton. So this time we need to understand how to make QML and Java interact. The main point here is the implementation under the Android system, not for the desktop platform J2SE JVM.
original article, against undeclared references. Original Blog address:http://blog.csdn.net/gamesdev/article/details/37997555
Qt5 has adapted and developed different modules for different platforms, such as Qt Windows Extras, qt Mac Extras, qt X11 Extras, QT Android Extras, etc. Here we focus on QT Android Extras, teach you how to use this module to achieve and Java interaction.
The Qtandroid Extras module contains three classes and a namespace. Qandroidactivityreceiver used to receive the results of Android activity; Qandroidjnienvironment used to access the JNI environment variable. The qtandroidjniobject corresponds to the normal Java class in the Dalvik virtual machine. In addition, there are some useful global functions in the Qtandroid namespace to interact with Java code, such as using qtandroid::androidactivity () to get the activity currently running.
My independent game "take Medicine" is a mobile advertising, mobile advertising SDK is a jar package, it seems unable to integrate into my project, but fortunately there is the QT Android Extras module, I use this module plus QML and C + + interaction, finally let the integration to achieve. I'm using a method like this to implement QML calling Java code:
In the three methods described in the previous article, I chose the method of invoking C + + registered singleton functions using QML to invoke C + + code, and then call Java's Local method through Qtandroidjniobject::callmethod (). To implement such a feature, you must call the Android method in this function in addition to the registered QML Singleton that is described in the previous article. The following is the implementation of the Sdactivitydelegatesingleton class:
SDActivityDelegateSingleton.h
#ifndef adsingleton_h#define adsingleton_h#include <qobject>//Android: Binding superdoctoractivity, calling class for the advertising API Sdactivitydelegatesingleton:public qobject{ q_objectpublic: Sdactivitydelegatesingleton (QObject* pParent = q_nullptr): qobject (pparent) { } q_invokable void call (const qstring& methodName);}; #endif//Adsingleton_h
SDActivityDelegateSingleton.cpp
#include "SDActivityDelegateSingleton.h" #if defined (q_os_android) &&!defined (Q_OS_ANDROID_NO_SDK) #include <qtandroid>void sdactivitydelegatesingleton::call (const qstring& methodName) { const qandroidjniobject& activity = qtandroid::androidactivity (); Qdebug ("Method name is:%s", qprintable (MethodName)); Activity.callmethod<void> (Qprintable (MethodName));} #elsevoid sdactivitydelegatesingleton::call (const qstring& methodName) { qdebug ("The desktop does not Implement calling Method:%s ", qprintable (MethodName));} #endif
Then in is Superdoctoractivity.java:
Superdoctoractivity.java This Java file overrides Android's Activitypackage Com.jcystudio.superdoctor;import Android.os.Bundle; Import Android.util.log;import Android.widget.linearlayout;import Android.widget.framelayout;import Android.view.gravity;import Org.qtproject.qt5.android.bindings.qtactivity;......public class SuperDoctorActivity Extends qtactivity{ @Override public void OnCreate (Bundle savedinstancestate) { super.oncreate ( Savedinstancestate); ... Smartbannermanager.init (this); } ... public void Showsmartad () { smartbannermanager.show (this);} }
In this way, the Java code can be called through QML, and the Calling method is:
... function prepare () {Sdactivity.call ("Showsmartad")//← call Java method this.focus = true;//Get key focus, You can use the key to control controller.clearblocks (); Controller.setupgame (mode, difficulty, map, actions, colors); headersprite.currentframe = 2;// The villain points to the usual position readymask.visible = True;backgroundmusic.play ();} ......
Using such a method can call the third-party jar package code, I would like to call the so library code is also possible.
By the way, in fact, my independent game is not called "take medicine" in the beginning, called "Super Sterilization Doctor", which also has a period of time my game name is called "Super Doctor". After catching up on the internet to take medicine on this joke, I changed my game name to "medicine". I cut a picture for the game every time I develop a milestone. One months down, cut a lot of pictures. Below I put these pictures together, make a gallery, for everyone to watch.
"Take the medicine": here