0. Write at the front
I refer to An Xiaohui Warrior's blog post, did the QT on Android GSP related experiments, in order to back a rainy day, so record down.
1. Qt on Android GPS system flow
Figure 1. System flowchart
1, the system includes two levels: one for Qt-based UI, a button to start the GPS (Qpushbutton), and a text field to display the GPS signal (Qtextbrowser), and the second is the activity-based GPS service, which provides a GPS start-up, GPS signal escalation and other services. Two levels of interaction and C + + and Java interaction are implemented through JNI.
The overall clue of the system is as follows: in the QT layer, when the button is clicked, the slot function Startgps is triggered, the function calls the activity layer method Calledbycpp, and the Calledbycpp method sends the message to the MES handle, the message type is Msg_ After Str_gps_loc,handle receives the message, the call function Startamap initiates GPS positioning. When the GPS is started, the location's real-time information is published in the function onlocationchanged () function, which sends the location information to the MSG handle via a message, where the message type is Msg_rpt_gsp_inf, Handle call the native method Reportgpsinfo, you can send the message to the QT layer.
2. Create Android Project
Figure 2 Creating a project
The project is named Qtandroidgps, and its path is E:\WORK\C++\QT, and after the project is created, the directory structure is as follows:
Figure 3. Project initial File
In the above auto-generated directory, add folder to Android:
Figure 4. Add Folder after Android
Folder Android contains the following content:
Figure 5. Folder for Android content
As you can see, the directory structure is similar to the Android project generated through Eclipse, with fewer directories such as assets,bin,gen,res, and files such as project.properties.
The project can be used by Baidu and the German library to locate, its related jar packages are Baidulbs_android.jar and Android_location_v1.1.2.jar, both of which are put into the e:\work\c++\qt\qtandroid\ Android\libs.
Also, after building the project QTANDROIDGPS, QT automatically generates a directory under path e:\work\c++\qt\:
E:\work\c++\qt\build-qtandroidgps-android_for_armeabi_v7a_gcc_4_8_qt_5_4_0-debug, the directory contains the following files:
Figure 6. Project catalog generated after QT construction project
Among them, the Android-build directory contains the QT-built Android project, which is the complete Android project:
Figure 7. The android Engineering catalog generated after QT construction works
It can be found that the files that we created previously were e:\work\c++\qt\qtandroid\android\ in addition to Anroidmanifest.xml, and other files including jar packages and Java files were copied into:
Build-qtandroidgps-android_for_armeabi_v7a_gcc_4_8_qt_5_4_0-debug.
3. List of engineering documents
3.1 E:\work\c++\qt\qtandroidgps\android\androidmanifest.xml
......
Figure 8. Androidmanifest.xml
This file is like the 4 important points of the callout:
①-> to the package path for Java files in E:\WORK\C++\QT\QTANDROIDGPS\ANDROID\SRC
②-> to put Baidu and gold pack up, must join Api-key
③-> class name corresponding to activity
④-> need to turn on GPS location-related permissions
3.2.1 E:\work\c++\qt\qtandroidgps\mainwindow.h
#ifndef mainwindow_h#define mainwindow_h#include <QMainWindow> #include <qpushbutton>namespace Ui {class MainWindow;} Class Mainwindow:public qmainwindow{ q_objectpublic: explicit MainWindow (Qwidget *parent = 0); ~mainwindow ();p rotected: bool Event (qevent *e);p rivate: ui::mainwindow *ui; Qpushbutton *btn; Click Start gpsprivate Slots: void Startgps ();//btn corresponding click Slot function}; #endif//Mainwindow_h
3.2.2e:\work\c++\qt\qtandroidgps\Mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QAndroidJniObject> #include "CustomEvent.h" Mainwindow::mainwindow (Qwidget *parent): Qmainwindow (parent), UI (new Ui::mainwindow) {ui->setupui (this); BTN = new Qpushbutton ("Start", this); Btn->setgeometry (Qrect (10, 10, 100, 50)); Connect (BTN, SIGNAL (clicked ()), this, SLOT (Startgps ()));} Mainwindow::~mainwindow () {Delete UI;} void Mainwindow::startgps () {//qandroidjniobject javaaction = qandroidjniobject::fromstring (URL); Qandroidjniobject::callstaticmethod<void> (/* Call the Java method, refer to Qt's Help documentation for details */"Com/mtn/mes/gpsservice", "CA Lledbycpp "," () V ");} BOOL Mainwindow::event (qevent *e) {if (e->type () = = Customevent::eventtype ()) {/* Match on Custom event type */E->accept () ; Customevent *sce = (customevent*) e; _resultview->settext (SCE->M_ARG2); Ui->textbrowser->settext (SCE->M_ARG2); /* Display GPS information */return true; } return Qwidget::eveNT (e);}
3.3.1e:\work\c++\qt\qtandroidgps\CustomEvent.h
#ifndef customevent_h#define customevent_h#include <QEvent> #include <qstring>class customevent:public Qevent{public: customevent (int arg1 = 0, const QString &ARG2 = QString ()); ~customevent (); Static Type EventType (); int m_arg1; QString m_arg2;private: static Type m_evtype;}; #endif//Customevent_h
3.3.2e:\work\c++\qt\qtandroidgps\CustomEvent.cpp
#include "CustomEvent.h" qevent::type Customevent::m_evtype = (qevent::type) qevent::none; customevent::customevent (int arg1, const QString &arg2) : Qevent (EventType ()), M_arg1 (arg1), M_arg2 (arg2) {} Customevent::~customevent () {}qevent::type customevent::eventtype () { if (M_evtype = = Qevent::none) { m_ Evtype = (qevent::type) registereventtype (); /* Register a custom event to return a custom event type with a value of QT assigned M_evtype */ } return m_evtype;}
3.4e:\work\c++\qt\qtandroidgps\CustomEvent.cpp
#include "mainwindow.h" #include <QApplication> #include <QDebug> #include <jni.h> #include < qandroidjnienvironment> #include <QAndroidJniObject> #include "CustomEvent.h" qobject *main_window = 0;/* This function is called by the Java Local method */static void Reportgpsinfo (jnienv *env, jobject thiz,int result, jstring data) {QString qstrdata; const char *nativestring = env->getstringutfchars (data, 0); Qstrdata = nativestring; Env->releasestringutfchars (data, nativestring); Qcoreapplication::p ostevent (Main_window, new customevent (result, qstrdata)); /* Send event (carry GPS information) to main window, custom event type */Qdebug () << qstrdata;} BOOL Registernativemethods () {Jninativemethod methods[] {{"Reportgpsinfo", "(iljava/lang/string;) V", (void*) Rep Ortgpsinfo}/* Register local method, Java can call, details see QT Help document */}; const char *classname = "com/mtn/mes/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); MainWindow W; Customevent::eventtype (); Registering custom events, generating custom event Types Registernativemethods (); Register Local method W.show (); Main_window = qobject_cast<qobject*> (&w); return a.exec ();}
3.5E:\work\c++\qt\qtandroidgps\android\src\com\mtn\mes\extendsqtnative.java
Package Com.mtn.mes;import Java.lang.string;public class extendsqtnative{public native void Reportgpsinfo (int result, String content);}
3.6e:\work\c++\qt\qtandroidgps\android\src\com\mtn\mes\Gpsservice.java
Package Com.mtn.mes;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;import Android.widget.toast;import Java.util.Date;import Android.location.location;import Android.location.locationlistener;import Android.location.LocationManager; Import Com.amap.api.location.amaplocation;import Com.amap.api.location.amaplocationlistener;import Com.amap.api.location.locationmanagerproxy;import Com.amap.api.location.locationproviderproxy;import Com.baidu.location.bdlocation;import Com.baidu.location.bdlocationlistener;import Com.bAidu.location.locationclient;import Com.baidu.location.locationclientoption;import Com.baidu.location.locationclientoption.locationmode;public class Gpsservice extends org.qtproject.qt5.android.bindings.qtactivity{private static Gpsservice m_instance; Private final static String TAG = "Gpsservice"; Private final static int msg_str_gps_loc = 0; Private final static int msg_rpt_gps_inf = 1; Locating related//////////////////// Private Locationmanagerproxy Amapmanager; Public Gpsservice () {m_instance = this; } public static void Calledbycpp () {System.out.println ("[0]hello world!"); Message msg = new Message (); Msg.what = Msg_str_gps_loc; M_instance.handler.sendMessage (msg); Message trigger, start GPS positioning//m_instance.handler.sendemptymessage (0); Message triggered, start GPS location} public static void Calledbycpp (int arG0) {System.out.println ("[1]hello world!"); private void Startamap () {Amapmanager = Locationmanagerproxy.getinstance (this); /* * Mamaplocmanager.setgpsenable (FALSE); * 1.0. Version 2 new method, set true to indicate that the hybrid location contains GPS positioning, false for pure network location, the default is true location * API positioning using GPS and Network hybrid positioning method *, the first parameter is the positioning provider , the second parameter is the shortest time is 2000 milliseconds, the third parameter distance interval unit is the meter, the fourth parameter is the location listener */Amapmanager.requestlocationupdates (Locationproviderproxy.ama Pnetwork, Mamaplocationlistener); } private Amaplocationlistener Mamaplocationlistener = new Amaplocationlistener () {@Override public void Onstatuschanged (String provider, int status, Bundle extras) {} @Override public void Onproviderenab LED (string provider) {} @Override public void onproviderdisabled (string provider) {} @Override public void Onlocationchanged {} @Override public void Onlocatio NchangeD (amaplocation location) {if (location! = null) {Double Geolat = Location.getlatitude (); Double geolng = Location.getlongitude (); String Citycode = ""; String desc = ""; Bundle Locbundle = Location.getextras (); if (locbundle! = null) {Citycode = locbundle.getstring ("Citycode"); desc = locbundle.getstring ("desc"); } String str = ("Location OK: (" + geolng + "," + Geolat + ")" + "\naccuracy : "+ location.getaccuracy () +" Meter "+" \npositioning: "+ location.getprovider () +" \nposi Tioning time: "+ New Date (Location.gettime ()). toLocaleString () +" \ncity coding: "+ Citycode + "\nlocation Description:" + desc + "\nprovince:" + location.getprovince () + "\ncity:" + Location.getcity () + "\ndIstrict (county): "+ location.getdistrict () +" \nregional Coding: "+ location.getadcode ()); Toast.maketext (Getapplicationcontext (), "German position \ n" + str, toast.length_short). Show (); Send location information to handler, hander to QT message msg = new Message (); Bundle data = new bundle (); Data.putstring ("value", str); Msg.setdata (data); Msg.what = Msg_rpt_gps_inf; Handler.sendmessage (msg); } } }; Private Handler Handler = new Handler () {@Override public void Handlemessage (Message msg) {Swit CH (msg.what) {case MSG_STR_GPS_LOC://Message type is start GPS positioning m_instance.startamap (); Break Case Msg_rpt_gps_inf://Message type is on send GSP information extendsqtnative m_nativenotify = new Extendsqtnative (); Bundle data = Msg.getdata (); System.out.println (Data.getstring ("value")); M_nativenotify.reportgpsinfo (0, data.getstring ("value")); Drop the C + + method break; Default:System.out.println ("msg type error!"); Break } } };}
Click to download the source code
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
QT on Android GPS signal acquisition