Many friends in the Forum and QQ Group to ask this, today there is time to write a simple example.
The function is very simple, allows you to enter a Web page address, use Java download class library to download and display it with Qtextedit.
All rights reserved: Foruok. Reprint Please specify source: Http://blog.csdn.net/foruok.
Effect Show
The initial effect is shown in 1:
Figure 1 Usejar Example Initial effect
Figure 2 is the effect of downloading to the corresponding page after clicking the "GET" button:
Figure 2 Download page success
The download section, in order to show how to use the jar package, I used the asynchttpclient, refer to my blog: Android Open source framework asynchttpclient (android-async-http) used.
Project creation
Refer to the Qt on Android: text-to-detail Hello world whole process, nothing special to say.
Added "QT + = Androidextras" in Pro file.
Create a androidmanifest,package named An.qt.useJar.
All rights reserved: Foruok. Reprint Please specify source: Http://blog.csdn.net/foruok.
Add Java source Code
You can edit the Java source code in any text editor, and then through the QT Creator Project View added to the project, the other files where the right mouse click, choose to add existing files. Refer to the following pictures.
Figure 3 Adding the Java Source right-click menu
Figure 4 Add Java source file selection Java source file
Figure 5 Adding Java source files ok
Modify the Androidmanifest to change the Android:name property value of the activity tag to An.qt.useJar.ExtendsQtWithJava. This is necessary because the activity that our Extendsqtwithjava.java implements is the name.
Well, the Java code is added to the end.
Add a third-party jar package
This is nothing to say, put it in the Android/libs directory. Look at the picture:
Figure 6 putting the jar package
As long as the location is set, Qt Creator will pack the jar package into the APK when compiling the project.
Java source code using jar package
This is the content of Java programming, import the package name, and then use it.
SOURCE Analysis
Let's look at the Java side code.
Java code
Extendsqtwithjava.java:
Package An.qt.usejar;import Java.lang.string;import Android.content.context;import android.content.intent;import Android.app.pendingintent;import Android.os.handler;import Android.os.message;import Android.util.Log;import Android.net.connectivitymanager;import Android.net.networkinfo;import Android.net.uri;import Android.location.criteria;import Android.provider.settings;import Android.os.bundle;import android.os.Environment ; Import Java.io.file;import Com.loopj.android.http.asynchttpclient;import Com.loopj.android.http.asynchttpresponsehandler;public class Extendsqtwithjava extends org.qtproject.qt5.android.bindings.qtactivity{private static Extendsqtwithjava m_instance; Private final static String TAG = "EXTENDSQT"; private static String M_pageuri = null; private static Handler M_handler = new Handler () {@Override public void Handlemessage (Message msg) { Switch (msg.what) {case 1:if (M_pageuri = = null) { M_pageuri = (String) msg.obj; M_instance.downloadtext (M_pageuri); }else{m_instance.notifyqt (0, (String) msg.obj, "Downloader is Busy now!"); } break; }; } }; Public Extendsqtwithjava () {m_instance = this; } public static int networkstate () {Connectivitymanager conman = (Connectivitymanager) M_instance.getsystemservi CE (context.connectivity_service); return Conman.getactivenetworkinfo () = = null? 0:1; } public static Asynchttpclient M_httpc = new Asynchttpclient (); public static extendsqtnative m_nativenotify = null; public void Downloadtext (String uri) {log.d (TAG, "Start Downloadtext"); M_httpc.get (URI, NULL, new Asynchttpresponsehandler () {@Override public void onsuccess (String data) { NOTIFYQT (1, M_pageuri, data); M_pageuri = null; } @Override public void OnFailure (Throwable e, String data) {NOTIFYQT ( -1, M_pageuri, data); M_pageuri = null; } }); } public static void Downloadwebpage (String uri) {log.d (TAG, "downloadwebpage"); M_handler.sendmessage (M_handler.obtainmessage (1, URI)); } private void notifyqt (int result, string uri, string data) {if (m_nativenotify = = null) {M_nativ enotify = new Extendsqtnative (); } m_nativenotify.ondownloaded (result, URI, data); }}
Extendsqtnative.java:
Package An.qt.usejar;import Java.lang.string;public class extendsqtnative{public native void ondownloaded (int result, string URL, string content);}
The basic idea is Jiangzi:
QT calls Java's Downloadwebpage,java code to download a Web page using Asynchttpclient, and then calls Extendsqtnative to notify QT C + + code.
C + + code
In two parts, part of the implementation of the JNI method. The other part is the method that calls the Java class.
Implement the JNI method and register
First look at the JNI implementation corresponding to the extendsqtnative, in the Main.cpp, are listed:
#include "widget.h" #include <QApplication> #include <QAndroidJniEnvironment> #include < qandroidjniobject> #include <jni.h> #include ". /simplecustomevent.h "#include <qdebug>qobject *g_listener = 0;//Result:-1 failed; 1 success; 0 busy;static void ondownloaded (jnienv *env, jobject thiz,int result, jstring URI, jstring data) {QString qstrdata; const char *nativestring = env->getstringutfchars (data, 0); Qstrdata = nativestring; Env->releasestringutfchars (data, nativestring); Qcoreapplication::p ostevent (G_listener, new simplecustomevent (result, Qstrdata));} BOOL Registernativemethods () {Jninativemethod methods[] {{"ondownloaded", "(iljava/lang/string; ljava/lang/string;) V ", (void*) ondownloaded}}; const char *classname = "an/qt/usejar/extendsqtnative"; Jclass Clazz; Qandroidjnienvironment env; Qandroidjniobject Javaclass (classname); Clazz = Env->getobjectclass (javaclass.object<jobject> ()); Qdebug () << "Find Extendsqtnative-" << clazz; BOOL result = FALSE; if (clazz) {jint ret = env->registernatives (Clazz, methods, sizeof (methods)/sizeof (methods[0]); Env->deletelocalref (Clazz); Qdebug () << "registernatives return-" << ret; result = ret >= 0; } if (Env->exceptioncheck ()) env->exceptionclear (); return result;} int main (int argc, char *argv[]) {qapplication A (argc, argv); Simplecustomevent::eventtype (); Registernativemethods (); Widget W; G_listener = qobject_cast<qobject*> (&w); W.show (); return a.exec ();}
Registers the Jni method and sets a global object to receive notifications. Specifically, refer to QT help to understand.
Calling the Java method
The call to the Java method is in Widget.cpp. Look directly at the code.
Widget.h:
#ifndef widget_h#define widget_h#include <QWidget> #include <QLineEdit> #include <qtextedit># Include <qlabel>class widget:public qwidget{ q_objectpublic: Widget (Qwidget *parent = 0); ~widget (); BOOL Event (qevent *e);p ublic slots: void Onget ();p rivate: qlineedit * m_urledit; Qtextedit * M_resultview; Qlabel * M_statelabel;}; #endif//Widget_h
are interface-related, there is nothing to say. See Widget.cpp:
#include "widget.h" #include <QVBoxLayout> #include <QPushButton> #include ". /simplecustomevent.h "#include <QAndroidJniObject> #include <qandroidjnienvironment>widget::widget ( Qwidget *parent): Qwidget (parent) {Qvboxlayout *layout = new Qvboxlayout (this); Qhboxlayout *getlayout = new Qhboxlayout (); Layout->addlayout (GetLayout); M_urledit = new Qlineedit ("Http://blog.csdn.net/foruok"); Getlayout->addwidget (M_urledit, 1); Qpushbutton *getbutton = new Qpushbutton ("GET"); Getlayout->addwidget (Getbutton); Connect (Getbutton, SIGNAL (clicked ()), this, SLOT (Onget ())); M_resultview = new Qtextedit (); M_resultview->setreadonly (TRUE); Layout->addwidget (M_resultview, 1); M_statelabel = new Qlabel (); Layout->addwidget (M_statelabel);} Widget::~widget () {}bool widget::event (qevent *e) {if (e->type () = = Simplecustomevent::eventtype ()) {e-> Accept (); Simplecustomevent *sce = (simplecustomevent*) e; Switch (SCE->M_ARG1) {case 1:m_resultview->settext (SCE->M_ARG2); M_statelabel->settext ("success!"); Break Case 0:m_resultview->settext (SCE->M_ARG2); M_statelabel->settext ("failed!"); Break Case-1: M_statelabel->settext (SCE->M_ARG2); Break } return true; } return Qwidget::event (e);} void Widget::onget () {#ifdef WIN32 m_resultview->settext ("Sorry, Just for android!"); #elif defined (ANDROID) QString url = m_urledit->text (); Qandroidjniobject javaaction = qandroidjniobject::fromstring (URL); Qandroidjniobject::callstaticmethod<void> ("An/qt/usejar/extendsqtwithjava", "D Ownloadwebpage "," (ljava/lang/string;) V ", Java Action.object<jstring> ()); M_statelabel->settext ("Downloading ..."); #endif}
Calling Java code in the Onget () slot is simple and does not explain. See the QT Help manual for a description of the Qandroidjniobject class in question.
OK, this is the end.
All rights reserved: Foruok. Reprint Please specify source: Http://blog.csdn.net/foruok.
My Qt on Android series articles:
- Qt on Android: text-to-detail Hello world whole process
- Introduction to the development of Qt 5.2 for Android under Windows
- Qt for Android deployment process Analysis
- QT on Android: output qt Debug information to Logcat
- Qt on ANDROID:QT 5.3.0 released for Android improvement instructions
- Qt on Android episode 1 (translation)
- Qt on Android episode 2 (translation)
- Qt on Android episode 3 (translation)
- Qt on Android episode 4 (translation)
- Qt for Android compiled pure C project
- Windows under Qt for Android compiled Android C language executable program
- Qt on Android:android SDK installation
- Qt on android:http download and JSON parsing
- Qt on Android Settings app is named Chinese
- QT on Android: let Qt Widgets and QT Quick apply full screen display
- Qt on Android: How to adapt to different screen sizes
Qt on Android: Using JNI with integrated jar package